2

I want to create a jQuery statement that looks like this:

$('.total_999').html("something");

However, 999 comes from variable called storeNo.

How can I construct this statement dynamically?

1
  • Can you come up with a better selector for getting total_999? can you show some html with the total_999 element? Commented Aug 20, 2010 at 20:41

2 Answers 2

7

$('.total_'+storeNo).html("something");

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

3 Comments

that would resolve to : $('.total_'+storeNo).html("something");
No it would not, @roger rover. You say that the variable named "storeNo" contains 999. You're not making the mistake of trying to do this in an "eval()" are you? Don't do that.
@Ender Hey thanks, Pointy to. And Roger Rover why is it not working? Post more code this should work.
1

total_999 in your example is a class. Since it appears to be unique (or likely so), then it should probably be the id, and use a more generic class.

for example:

<div id="total_999" class="total">...</div>

then you can use the id, or the class to refer to the element

$('#total_'+storeNo); //get the element

or

$('.total'); //get all of the totals

A more complete example might be:

function setTotal(storeNo, total) {
  $('#total_'+storeNo).html(total);
}

setTotal(999,'$1,276');

3 Comments

right. It is id, but this ...$('#total'+store_number); does not resolve to #totall999
roger, store_number=999;$('#total_'+store_number); resolves to the element with id "total_999"
changed the variable name to match the one you used in the question

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.