jquery replace an ID with variable
I have an id="test" and would like to have it replaced by the variable:PitchesRemaining: http://jsfiddle.net/xtian/G7Qdp/
HTML:
<div class="wrap wrap-widgets relative">
<div class="speedometer" id="test">6</div>
<span id="AveragePitches">55</span>
<span id="PitchesToday">15</span>
</div>
Jquery:
$(function(){
var AveragePitches = parseInt($("#AveragePitches").text());
var Buffer = AveragePitches * .2;
var PitchesMax = AveragePitches + Buffer;
var PitchesToday = parseInt($("#PitchesToday").text());
var PitchesRemaining = PitchesMax - PitchesToday;
$("#test").change(function() {
开发者_如何转开发 PitchesRemaining = $(this).val();
}).change();
});
You can just change the Id using jQuery's attr
method:
obj.attr("id", PitchesRemaining);
I'm not sure what you're trying to do though. If you're trying to use the ID to track a variable, then perhaps you should take a look at .data
instead.
EDIT:
After reading your comment in the question; to change the text of the div to the value of PitchesRemaining
, just use text
:
$("#test").text(PitchesRemaining);
Or html
:
$("#test").html(PitchesRemaining);
To change the value of a <div>
do this:
$('#test').html(PitchesRemaining);
Note: .change()
events are only for input elements, not for divs.
精彩评论