views:

55

answers:

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?

+7  A: 

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

Iznogood
that would resolve to :$('.total_'+storeNo).html("something");
roger rover
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.
Pointy
@roger rover - proof that this works: http://jsfiddle.net/ExpNj/
Ender
@Ender Hey thanks, Pointy to. And Roger Rover why is it not working? Post more code this should work.
Iznogood
+1  A: 

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');
Jonathan Fingland
right. It is id, but this...$('#total'+store_number); does not resolve to #totall999
roger rover
roger, `store_number=999;$('#total_'+store_number);` resolves to the element with id "total_999"
Jonathan Fingland
changed the variable name to match the one you used in the question
Jonathan Fingland