Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is innerHTML on input elements?

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?

like image 277
temporary_user_name Avatar asked Dec 16 '13 05:12

temporary_user_name


People also ask

Why is innerHTML used?

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 innerHTML method in JavaScript?

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.

How do you find the innerHTML of an 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.

What is innerHTML property in DOM?

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.


3 Answers

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

like image 76
Ajith S Avatar answered Sep 28 '22 01:09

Ajith S


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.

like image 41
Shoaib Chikate Avatar answered Sep 28 '22 01:09

Shoaib Chikate


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.

like image 34
Jeroen Avatar answered Sep 28 '22 02:09

Jeroen