I am feeling really stupid and not sure if doing something really dumb what... so excuse me in advance.
I have the following HTML:
<input type="text" value="0" name="pcscubes" id="pcscubes">
I have the below jquery syntax:
var cubes=123.2;
$("#pcscubes1").val(cubes);
var test=$("#pcscubes1").val();
alert(test);
When the jquery executes, the input box will display 123.2 and the alert will be 123.2.
if you inspect the html object though, the value is still 0?
so the display of the input is different from what the real value is. so when I submit this form it posts 0 rather than 123.2
Now I have built a whole web app with this syntax and works perfectly. today it just stops working? I have restarted browser, checked old code that was working, logged off and on and still it does the same.
web app obviously has jquery loaded and jquery ui on this form.
using codeigniter?
any ideas as I am banging my head over something so stupid....
Out of interest I created the below fiddle and it is doing the same?
http://jsfiddle.net/49nqU/
why is the value 0 when it is showing 123.2?
Thanks as always,
This is a case of attributes vs. properties.
What you are seeing in the inspector is the value attribute, which does not update when you programmatically update a field's value property.
Pay no attention to what the DOM inspector says; it'll only show the value as it was when the page loaded (unless you explicitly change the field's value attribute rather than, as jQuery's val() does, its value property.)
Further reading: http://jquery-howto.blogspot.co.uk/2011/06/html-difference-between-attribute-and.html
Some attributes are closely tied to their property counterparts. That is to say, updating the property also updates the attribute. This is the case, for example, with the class attribute and its className property counterpart. value, however, is different; updating the property does not update the attribute (but, just to confuse things further, updating the attribute does update the property!)
val() is changing the value property, not attribute. The value attribute is initialized at the loading of the page. Once you change text field the value property and attribute have differnet values.
But you can use plain JS for changing the value attribute also.
Try:
var cubes=123.2;
document.getElementById('qty').setAttribute('value',cubes);
var test=$("#qty").val();
alert(test);
DEMO
Related
I have created JS script that capture user inputs and publish them in another hidden input document.
I confirmed it works because I made the hidden input visible and publish all the input as delimited string.
So far it works fine
but when I try to use the property document in another textarea, ironpython etc.. within the same DXP it is returning blank, even though I can see the string published in the previous text area.
I used this html tags ...
for input property to display the captured data.
jQuery to capture all the inputs
inval=$.....
......
....
then used this to publish them in the input field $('#dfdklsfksldfkslfs').text(inval).blur()
so far all works fine.
but after this when trying to use the document property in textarea, irontpython, within the same DXP, it is returning (BLANK) even though I can see them published in the textarea.
am I missing any steps? do I need to reassign some features?
also I have tried $('#dfdklsfksldfkslfs').val(inval).blur() this won't even publish the data in the inputfield.
here is the update with code
html
<div id='dispInput'> <spotfirecontrold id='dfdklsfksldfkslfs'></div>
jquery
$('button')click(function(){
inval=$('input').map(function(){
return $(this).val(); }).get().join('-');
$('#dfdklsfksldfkslfs').text(inval).blur() //this publish the result but don't assign the data to the document property
})
I am completely lost.
thanks a lot
Try setting timeout in the function (500ms will do).
$('button')click(function(){
setTimeout(function (){
inval=$('input').map(function(){
return $(this).val(); }).get().join('-');
$('#dfdklsfksldfkslfs').text(inval).blur()
}), 500
});
Hope this helps.
I'm not new to HTML but I always get confused with input elements, below is some html:
<input id="firstTest" />
<input id="secondTest" value="Hello"/>
so for the firstTest input, I typed some text like "Hi",then do:
let firstInput = document.getElementById('firstTest');
console.log(firstInput.value) //produces "Hi"
but for the secondTest input, I did the same thing but the value is always "Hello" and I can't type anything into the input field.
So my questions are:
Q1-why for the firstInput, I didn't specify a value attribute but the value can change depending on what I typed?
Q2-when I type sth into the field, what actually happened? How browser display the thing I type?
does input object in DOM get its value property updated automatically then the browser displays the latest value on screen?
Q3-if input object in DOM get its value property updated automatically, why on the secondInput that has a value attribute couldn't get its value property updated automatically?
Q1-why for the firstInput, I didn't specify a value attribute but the value can change depending on what I typed?
The value HTML attribute serves only for the purpose of defining an initial value. input elements have a Javascript API, and in this, there is a value property (which you are using in your console.log). This value property constantly reflects whatever is in the input. If you want to know what is in the value attribute, there's two ways to find out:
el.getAttribute('value');
or, using the aforementioned API
el.defaultValue;
Q2-when I type sth into the field, what actually happened? How browser display the thing I type? does input object in DOM get its value property updated automatically then the browser displays the latest value on screen?
That's a matter of how the browser handles it internally. This is an implentation detail which is never relevant for the web developer. It is laid out in the specification for that element.
Q3-if input object in DOM get its value property updated automatically, why on the secondInput that has a value attribute couldn't get its value property updated automatically?
Given the code you show here, that cannot be the case. The only ways to disable typing in an input is if the input has a disabled or readonlyattribute, or if someone has added a keydown listener that calls event.preventDefault().
If this does not fully answer your question, please leave a comment stating what is remaining unclear or unanswered.
I tried to update input element's value using .val() and it turned out that it doesn't affect the value="" attribute. After reading some topics here on stackoverflow, it turned out that these are not the same.
I started changing them both to be sure I do not do any mistake there:
$("#elementid").val(variableNumber).attr('value', variableNumber);
Now, lets say that I am sending the value of my input to validate it in php.
Which of these will be sent ff I make them different?
$("elementid").val(variable1);
$("elementid").attr('value', variable2);
Is there any rule on this? or any factor that makes one of these being sent as the actual "value" ?
Which of these will be sent ff I make them different?
The input's current value will be sent. The current value is reflected by the the value property. The value attribute represents the default value of the input, not its current value (and is reflected as the defaultValue property on the input). The default value is used to initialize the value when the input is created, and to reset it if you use the reset method of a form it's in.
Unless you want to change the default value, there's no need to set the value attribute, just the property. The property is what val changes, what changes when the user acts on the input, and what gets sent when the form is submitted.
When you are working with <input type='text'> I think you should use attr('value', variableNumber); instead of val(value).
val(value) is useful when working on a jQuery object containing elements like <input type="checkbox">, <input type="radio">, and <option>s inside of a <select>. More infomation here.
I'm trying to get the values of an input field using xPath to validade that the site is working as expected.
But some of the input tags are using jquery data-bvalidator and i cannot access the text/value on them.
You have an example page here:
bvalidator usage
If you try to put some text, for example "Alfa" on the first input box and try to access it, in debug mode, using:
$x('//*[#id="form1"]/div/div[1]/p[1]/input')
you will just get the element <input type="text" data-bvalidator="alpha,minlength[10],required">and not the "Alfa" value.
how can i get around this?
PS: This only works in chrome.
Getting the value property with xpath is not possible.
Wiht xpaht you can get the value attribute value - if there is one. But this would never refelect the changes of the input value (property).
$x('//*[#id="form1"]/div/div[1]/p[1]/input/#value')
To get the property value you can use (in Chrome Debugger):
$x('//*[#id="form1"]/div/div[1]/p[1]/input')[0].value
Update due to the mentioned of selenium in comment:
With selenium you can use ele.getAttribute("value") where ele is the first element from the xpaht search result.
Or something like:
driver.findElement(By.xpath(
'//*[#id="form1"]/div/div[1]/p[1]/input')).getAttribute("value");
Title pretty much says it all. I'm just wondering if anyone would know why the value binding on knockout doesn't create/set a value attribute on the input element in the document.
Per request, making my comment into an answer:
The value attribute on an <input value="foo"> element is the initial value of the field. Once active in the page, the .value property is the current value. Attribute and property are not the same.
If you have some flawed piece of code (e.g. the printing plugin) that you're trying to work with that is using the attribute instead of the property, then I supposed you could set the attribute to match the property before calling that printing plugin or you could path the printing plugin to correctly access the property.