I'm just trying to do this from the chrome console on Wikipedia. I'm placing my cursor in the search bar and then trying to do document.activeElement.innerHTML += "some text"
but it doesn't work. I googled around and looked at the other properties and attributes and couldn't figure out what I was doing wrong.
The activeElement
selector works fine, it is selecting the correct element.
Edit: I just found that it's the value
property. So I'd like to change what I'm asking. Why doesn't changing innerHTML
work on input elements? Why do they have that property if I can't do anything with it?
The innerHTML property can be used to write the dynamic html on the html document. It is used mostly in the web pages to generate the dynamic html such as registration form, comment form, links etc.
What is JavaScript innerHTML? The JavaScript innerHTML property sets the HTML contents of an element on a web page. InnerHTML is a property of the HTML DOM. innerHTML is often used to set and modify the contents of a <p> element.
Setting the innerHTML property of an element To set the value of innerHTML property, you use this syntax: element. innerHTML = newHTML; The setting will replace the existing content of an element with the new content.
The DOM innerHTML property is used to set or return the HTML content of an element. Syntax: It returns the innerHTML Property. Object.innerHTML. It is used to set the innerHTML property.
Setting the value
is normally used for input/form elements. innerHTML
is normally used for div, span, td and similar elements.
value
applies only to objects that have the value attribute (normally, form controls).
innerHtml
applies to every object that can contain HTML
(divs, spans, but many other and also form controls).
They are not equivalent or replaceable. Depends on what you are trying to achieve
First understand where to use what.
<input type="text" value="23" id="age">
Here now
var ageElem=document.getElementById('age');
So on this ageElem
you can have that many things what that element contains.So you can use its value
,type
etc attributes. But cannot use innerHTML because we don't write anything between input tag
<button id='ageButton'>Display Age</button>
So here Display Age
is the innerHTML content as it is written inside HTML tag button.
Using innerHTML on an input tag would just result in:
<input name="button" value="Click" ... > InnerHTML Goes Here </input>
But because an input tag doesn't need a closing tag it'll get reset to:
<input name="button" value="Click" ... />
So it's likely your browsers is applying the changes and immediatly resetting it.
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