4

I have a span in my HTML as follows, which I am assigning the text from code behind:

<span id="NewL1" visible="false"
    runat="server"><span runat="server" id="NewL"></span></span>

The text would be something like : "you have 3 notifications."

I want to change the numeric value in jQuery. I have tried this but nothing happens:

var notification = $("#NewL");
var numb = notification.text().match(/\d/g);
var finalTotalCount = parseInt(numb) - 1;
notification.text(notification.text().replace(numb, finalTotalCount ));

What I am doing wrong?

12
  • I wouldn't go this way. Instead of replacing, generate the whole string from scratch, then update the DOM: this way you can be more flexible [e.g. use singular when there is one notification only], and you don't mess with regexes for such a task. Commented Jan 3, 2013 at 12:40
  • When is your JS code running? Onload? Are you sure that by then the span will have content? Commented Jan 3, 2013 at 12:40
  • 1
    Your code is working here: jsfiddle.net/leniel/ykg62 Commented Jan 3, 2013 at 12:40
  • @moonwave99 i am doing this as for future changes one place needs to be changed :) Commented Jan 3, 2013 at 12:43
  • @Deleteman it runs on selecting a button Commented Jan 3, 2013 at 12:43

3 Answers 3

1

So you have this HTML code:

<span id="NewL1" visible="false" runat="server">
    <span runat="server" id="NewL">you have 3 notifications</span>
</span>

Then, your current JavaScript code just works provided that your dynamic text you have 3 notifications is being inserted in the right place, that is, within the inner span.

Here's the jsFiddle: http://jsfiddle.net/leniel/ykg62/2/

Sign up to request clarification or add additional context in comments.

Comments

1

Replace:

var finalTotalCount = parseInt(numb) - 1;

With:

var finalTotalCount = parseInt(numb[0]) - 1;

.match(/\d/g) returns an array of strings that match the regex. You'll want to get the first result of that array, assuming you only have 1 number in your text.

Edit

Apparently, parseInt uses the first element in the array, if a array is provided. So, technically, my suggestion isn't necessary.

5 Comments

Hi, I only have one number in the string. I can see the finalTotalCount decreasing but dont see it being replaced.
@Deleteman: Hm, I see. Apparently, parseInt uses the first element in the array, if a array is provided. Still, best practices, right? Sam1: your code appears to work. Did you copy all your relevant code to the question?
@Cerbrus didnt include the span inside another span see my edit of html please
@Sam1: that shouldn't matter.
@Deleteman +1 for suggestion...I think the first span is not visible or the fact that am assigning from code behind may be issue(although the text is there when the above jquery code runs)
1

Modify you code like this and try again

var notification = $("#NewL");
var numb = notification.text().match(/\d/g)[0];
var finalTotalCount = parseInt(numb) - 1;
notification.text(notification.text().replace(numb, finalTotalCount ));

Also, in your code:

<span id="NewL1" visible="false" runat="server">
    <span runat="server" id="NewL"></span>
</span>

You have span inside a hidden span(visible="false"). So won't be able to see any changes done. You can simply put your code like this and try again:

<span runat="server" id="NewL"></span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.