Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML input value attribute - same input, (apparently) different results

How does value attribute work for the html input tag after an edit via the inputbox?

example:

<input type="text" name="test" id="test" value="Hello world" />

this will show an input box with the text "Hello world". If I edit it inputing a new string into the rendered textbox (not via raw code), and then try to get his value using js like this

alert(document.getElementById('test').value)

I'll, correctly, get the NEW value. But if I inspect the element through chrome developer tools (or firebug or anything you prefer), I'll see the same "Hello world" string as it was in the beginning..

Which of the two is the right one? js value or chrome inspector?

Here's the Example Fiddle and here's a screenshotenter image description here

I came up to this while trying to find a solution to this problem: Classic shop situation, I have a table with X inputs tag , where an user can input the quantity of X items. I need to check if one or more values have changed since the previous value of each input: if the comparison between OLD and NEW val returns that the valuehas changed, I need to update the order. Otherwise there's no need to save/update.

I thought I could use attr. "value" to store the "old" value of the input, but probably was not the best choice..

Can anyone explain this behaviour? It's only a "refresh" problem or is there something else behind?

like image 330
BeNdErR Avatar asked Feb 15 '13 09:02

BeNdErR


People also ask

Which attribute of input tag accepts multiple values in the same field?

The multiple attribute is a boolean attribute. When present, it specifies that the user is allowed to enter more than one value in the <input> element. Note: The multiple attribute works with the following input types: email, and file.

How do I make input only accept certain values?

You can use the <input> tag with attribute type='number'. This input field allows only numerical values. You can also specify the minimum value and maximum value that should be accepted by this field. Save this answer.

What attribute is used differently for different input types?

The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button. For "text", "password", and "hidden" - it defines the initial (default) value of the input field.

Can a HTML input have 2 types?

No, a field element can't have multiple types. The only thing you should leave up to the user is inputting data - you select the types when designing the form. The user will pick which ones they want to fill out - unless you make them required. This can all be accomplished in HTML without scripting.


3 Answers

Both are right, they just show different things.

The DOM value property gives you the current value.

The HTML value attribute gives you the default value (the same as the DOM defaultValue property).

The Chrome DOM inspector shows you attribute values in the HTML view. You have to look at the property view to see the property values.

Screen shot of Chrome property view

like image 98
Quentin Avatar answered Oct 17 '22 02:10

Quentin


You can find information in this article.

the value seen in html is the default value the form is loaded with. If you want the DOM also to update then you have to use ele.setAttribute("value", "new value")

If you want to know if a element is changed, the best is to attach a onchange event to the input elements and mark some flag in the change handler to know if the form changed or not.

like image 34
sinduja ramaraj Avatar answered Oct 17 '22 03:10

sinduja ramaraj


You can assign a so-called proprietary attribute to the input; for example "oldval" so that you can later compare it with current value

like image 1
alijsh Avatar answered Oct 17 '22 01:10

alijsh