I am trying to change john to mike. I have no idea why its not working.
<span id="user">John</span>
I am trying this but not working i have no clue why not working.
function set() {
document['getElementById']('user')['value'] = Owner;
// owner value is mike
}
Just as it is possible to style content by wrapping a span tag around it, you can also manipulate your content by wrapping it in a span tag. You give it an id attribute and then select it by its id with JavaScript so you can manipulate it.
Use innerText is the best method. As you can see in the MDM Docs innerText is the best way to retrieve and change the text of a <span> HTML element via Javascript.
The <span> tag is an inline container used to mark up a part of a text, or a part of a document. The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.
Bookmark this question.
If you want to change the id, use
document['getElementById']('user').id = 'mike';
or, more classically,
document.getElementById('user').id = 'mike';
If you want to replace "John" (that is not the ID but the content of the span), do
document.getElementById('user').innerHTML = 'mike';
function set() {
document.getElementByID('user').innerHTML = Owner;
// owner value is mike
}
try:
function set() {
document.getElementById('user').innerText= Owner;
// owner value is mike
}
Where is Owner
declared, is it valid in your function scope?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With