Using a ajax request I want to change content of my div.
<div id="d1">202</div>
So I want to change the content to a different number.
$('d1').InnerText???
Also, say I wanted to increment the number, how could I do that? Do I have to convert to int?
Use the text function:
if your value is a pure text (like 'test') you could use the text() method as well. like this:
anyway, about the problem you are sharing, I think you might be calling the JavaScript code before the HTML code for the DIV is being sent to the browser. make sure you are calling the jQuery line in a <script> tag after the <div>, or in a statement like this:
this way the script executes after the HTML of the div is parsed by the browser.
Unless you're embedding html like
<b>blah</b>
I'd suggest using$("#di").text()
as it'll automatically escape things like <, > and &, whereas.html()
will not.Check out the jQuery documentation.
If you wanted to increment the number, you would do
P.S. Not sure if it was a typo or not, but you need to include the
#
in your selector to let jQuery know you are looking for an element with an ID ofdi
. Maybe if you come from prototype you do not expect this, just letting you know.