This question already has answers here:
What is the difference between properties and attributes in HTML?
(7 answers)
Closed 1 year ago.
I have found an <input type="text" ... element on a page, for which $('thatElement').val() and $('thatElement')[0].value return the value, the value is displayed in the rendered output, but there is no value="..." attribute on the element itself.
How can there be no value attribute in the HTML for an input element, but it still has a value and still displays its value?
Browser console:
> $('thatElement')[0].value
'The text that the page shows inside of the input'
> $('thatElement')[0].outerHTML
'<input autocomplete="off" type="text" readonly spellcheck="false">'
There's a difference between the source of a document and its DOM representation. The source file is static and won't change. The DOM might change. I think it depends on how you manipulate it.
In this example the input value is set but the DOM isn't updated: (run and inspect the result)
document.querySelector('input').value = 42;
<input />
In this example both the input and the DOM is updated:
document.querySelector('input').setAttribute('value', 'foobar');
<input />
Related
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 6 years ago.
I have two input type = range:
<input type="range" id="range_1" name="rangeA">
<input type="range" id="range_2" name="rangeB">
I want to get the values and put them in a function.
How can i do that?
first you need to select the input, thankfully you have separate ids on them,
Vanilla JS:
var element = document.getElementById('range1').innerHTML;
Here is a list of stuff you can then do with that element, like innerHTML
https://developer.mozilla.org/en-US/docs/Web/API/Element
I'm trying to retrieve the text that I've written in the onchange attribute of my input tag.
For eg:
<input type="text" id="test1" name="test1" onchange="some_js_func();" />
I want to retrieve the value of the onchange, i.e, "some_js_func();" on load of my html page.
I've tried the following to access this in the same way as we access any value for an hmtl element using getElementById() function:
var myvar = document.getElementById("test1").change;
However the above code returns undefined.
Is there any way to get the value of the onchange as a string using only javascript and not jQuery or other client side scripting language?
I want the following specific text only: "some_js_func();"
You just need to do:
document.getElementById("test1").getAttribute("onchange");
The getAttribute() method will get the value of an attribute for the element.
Just use .getAttribute():
//Out input element:
var myElem = document.getElementById("test1");
//Here, the appended text node has the same value as the string inputted into the onchange attribute in the HTML:
document.body.appendChild(document.createTextNode(myElem.getAttribute("onchange")));
<input type="text" id="test1" name="test1" onchange="some_js_func();">
This question already has answers here:
HTML - attributes vs properties [duplicate]
(2 answers)
Closed 8 years ago.
I have the below element:
<input type="text" value="" tabindex="1" size="15" onblur="UpCaseSearch(this)" name="name">
which is an empty text field.
I want to set text into that field and I can successfully do that using a command like that (when I say successfully I mean that I can see that field populated with text test:
window.frames[1].document.getElementsByName('name')[2].value = "test"
I have 2 questions however:
When I look at that element on the page I see that the value attribute is still empty "". Why is that? Where is the actual text that I can see in that field is coming from?
If I try the below command, it will actually set the value but then the field remains empty:
window.frames[1].document.getElementsByName('name')[2].setAttribute('value', 'test')
So it looks like that's not the same value in both cases. Is that right?
Try this:
window.frames[1].document.getElementsByName('srchsnam')[2].value = "Test"
Let me know if it worked!
In short, the value attribute in source-code (and dom) serves as 'defaultValue'.
For example, imagine this input-field (which is a common way to use this functionality):
<input type="text"
value="your name here"
onfocus="if (this.value===this.defaultValue) this.value='';"
onblur="if (this.value==='') this.value=this.defaultValue;" >
In other words, to set the default value in html-markup (<input value="your_value" type="text">) and from javascript you set the value-attribute (using the methods: var val=elm.getAttribute('value') and elm.setAttribute('value', val).
This is also why (as you have seen) these attributes propagate to the element's outerHTML (and thus the element's parent innerHTML).
However to get/set the element's current value property you use: var val=elm.value and elm.value=val.
Sidenote: Almost the same goes for a textarea: you can use .defaultValue on a textarea to retrieve it's textContent (but you'd override/change that by setting it's innerHTML) while you get/set the current value using elm.value.
Hope this helps!
This question already has answers here:
Get the value in an input text box
(13 answers)
Closed 8 years ago.
I am very new to programing so sorry if this question is to vague.
I have a simple html input field:
input class="draft" type="text"
and I have a javascript function (the function only accepts strings):
Message.send()
Using javascript/jQuery how do I take the string the user typed into the input field and place it into the Message.send() function (in string form)?
Thanks in advance for any help
jQuery:
Message.send($("input.draft").val());
javascript:
Message.send(document.querySelector("input.draft").value);
No jQuery needed. I'm not sure what you mean by place it into the Message.send() function (in string form), so I assume you get the value in the text input and use it as an argument in the Message.send() function.
Try this:
Message.send(document.getElementsByClassName("draft")[0].value);
Assuming your input field is something like this:
<input type="text" id="someInputField" name="someInputField" class="inputFields" />
You could do this
<script type="text/javascript">
//We use the field's id to refer to it and get to its value (the text in it):
var message = $('#someInputField').val();
//And then you might call the function like:
nameOfTheFuntion(message);
</script>
You would need to have jQuery libraries to make it work though, but you could do without them by replacing:
$('#someInputField').val();
with
document.getElementById('someInputField').val();
Give your <input> box an id for example
<input type="text" id="blah" />
In your javascript you are able to reference the <input> like so:
var strBlah = document.getElementById("blah").value;
Now you have the value typed into the <input> box so you would do the following:
Message.send(strBlah)
Assume you have input element:
<input id="aaa" type="text" value="unchanged" />
Then launch js script:
var e = document.getElementById("aaa");
e.value = "changed";
alert(e.defaultValue + "/" + e.value);
Result will be "unchanged/changed". Unfortunately, when your input element is hidden:
<input id="aaa" type="hidden" value="unchanged" />
...the same js script seem not to be working any more. Result is "changed/changed".
Is this a proper way? If so, why only hidden form elements act different?
The "defaultValue" property is only maintained in a way you apparently expect for "text", "file", and "password" fields.
Here is the relevant portion of the DOM spec.
I suspect the reason for this is that user activity on its own cannot change the value of hidden elements. If you want to preserve the initial values, run something at "load" or "ready" to stash the value somewhere.
For hidden input elements, defaultValue isn't actually implemented. The reason why you get the same result ast .value is because the browser your using is just defaulting.
See here for a discussion of this with Firefox.