I'd like to set text to dd element. I've tried to google that question, but found nothing.
Could you please tell me, how can I set(replace) text inside dd tag?
I've tried:
$('#dd_di').innerHTML = my_var;
$('#dd_di').innerText = my_var;
$('#dd_di').innerText = "3500";
but nothing changed.
console.log($('#dd_di'));
[<dd id="dd_di"></dd>]
I'd like to set text to dd element. I've tried to google that question, but found nothing.
Could you please tell me, how can I set(replace) text inside dd tag?
I've tried:
$('#dd_di').innerHTML = my_var;
$('#dd_di').innerText = my_var;
$('#dd_di').innerText = "3500";
but nothing changed.
console.log($('#dd_di'));
[<dd id="dd_di"></dd>]
Share
Improve this question
asked Jun 24, 2014 at 10:42
Aleksandr K.Aleksandr K.
1,41516 silver badges24 bronze badges
4 Answers
Reset to default 4You're trying to use DOM properties on a jQuery object. In jQuery, use .text()
or .html()
to set the text and raw HTML of an element respectively.
$('#dd_di').text(my_var);
$('#dd_di').html(my_var); // or if you need to set the raw HTML
innerHTML
and innerText
can be used, but you need to access the DOM element:
$('#dd_di').get(0).innerHTML = my_var;
// or no jQuery:
document.getElementById("dd_di").innerHTML = my_var;
use $('#element_id').html(), do:
$('#dd_di').html("your text");
demo:: jsFiddle
try this:
$(document).ready( function() {
$('#dd_di').text("custom text");
});
<dd id="dd_di">hello</dd>
http://jsfiddle/5RPaZ/
You can use the DOM properties on the DOM objects:
window.dd_di.innerHTML = my_var;
window.dd_di.innerText = my_var;
DOM elemnets with are available in the global (window
) objects. You can also be more explicit:
document.getElementById('dd_di').innerHTML = my_var;
document.getElementById('dd_di').innerText = my_var;
Both work without jQuery. Normally you'd consistently use either jQuery (see other answers) or native DOM api.