I have a javascript that I am trying to write to do a comparison with a list of objects. But first I need to pull the value from the below HTML.
<div class="no_icon" style="width:100%;display:-moz-deck;">
<input title="model1" onfocus="thtmlbSaveKeyboardFocus('product_type');" class="class1"
style="width:100%;" dir="ltr" name="product_type" id="product_type" maxlength="40"
onkeydown="if(htmlbEnterKey(event)==true){return
htmlbSL(this,2,'product_type:submitonenter','0')};" value="model1" disabled="disabled"></div>
my issue happens when I try to pull the information I need from the page. I have tried several versions of the "document.getElement" commands,(TagName,ID,Class) but I cannot seem to pull the information i need.
When I tried to see if i could even access the input I received either a null or undefined return. but when I do a
var test=document.getElementsByTagName(product_type.class1");
console.log(test);
I get a return of object# nodelist
After doing some digging into nodelists I have discovered that "product_type.class1" has an attribute of namedNodeMap. But nothing i seem to do can pull the value section from within the HTML.
What I need is a way to get the value of the "value="field.
I think you will have more success using querySelector instead of getElementsByTagName:
var input = document.querySelector("[name='product_type']");
console.log(input.value);
Related
I have come across a strange thing which I didn't find any reference for.
In JavaScript we use document.getElementById("elem").value to get the value of the element with the id="elem"
But I have seen some material which use elem.value straightway to get the value of the element with the id="elem". Previously I thought its a mistake, but when I use the code it works!!
Is it a valid code? How it works??
Please clarify.
when you using a html form element it like this <input type="text" id="name">
then js collects this input type value usingdocument.getElementById("elem").value
I've tried:
$(element).attr("value", newValue);
which changed the value when I inspect the element but it doesn't show when I look at the actual input. I've then tried to .trigger different stuff but it doesn't work. (I've also tried $(element).val(newValue)).
EDIT: Found the error, I imported an obj from the server in JSON format. When I parsed it, the property I wanted to use didn't work as expected in the val function. Only sometimes it worked. To fix it I, when parsing it, used eval() on the object property.
Try using jQuery's .val() method:
$(element).val(newValue);
Bootstrap should not be making any difference here.
Always check what you are passing to the val() (or any other) function and what that function expects as parameter, especially when using a specific unit or doing some calculations which might end up being string concatenations instead.
jQuery("input").val(456);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="input"></label>
<input type="text" class="form-control" id="elementID" placeholder="..." value="123">
I have loaded an HTML page and ran some javascript code on the console. Can any one explain me the behavior.
document.getElementById('tiers__0__.threshold1') gives me
<input id="tiers__0__.threshold1" type="text" class="field mandatory" data-name="tiers__0__.threshold1" data-domain="ANY" name="dynamicValues['tiers__0__.threshold1']" value="5" maxlength readonly>
document.getElementById('tiers__0__.threshold1').value gives me
""
document.getElementById('tiers__1__.threshold1') gives me
<input id="tiers__1__.threshold1" type="text" class="field " data-name="tiers__1__.threshold1" data-domain="ANY" name="dynamicValues['tiers__1__.threshold1']" value="10" maxlength readonly>
document.getElementById('tiers__1__.threshold1').value gives me
"10"
Why is the 1st DOM element not giving me the value. Both the DOM elements have same structure. I generate them using Handlebars "each" property.
P.S. I know sometimes the DOT in an id creates issue. Even tried using escape sequence (//). Nothing worked. Tried Jquery also
$('#tiers__0__\\.threshold1').val() (Used double slash. SO just displays 1) gives me
""
Question Updated after more research.
The earlier test was done on Chrome browser but after doing it on Firefox got different result.
$('#tiers__0__.threshold1')
Object[input#tiers__0__.threshold1.field property value = "" attribute value = "5"]
Can anyone explain what is property value and why it it getting appended only for my 1st text box as null. Is their any way i can make this same as my attribute value or i can remove it completely.
Note -: I am using handlebar template for rendering a JSON object. I have checked both the JSON and Handlebar consists of uniform data. i have a input tag which looks like this
<input id="{{name}}{{#if count}}{{count}}{{/if}}" type="text" class="field {{#if this.mandatory}} mandatory{{/if}}" data-name="{{name}}" data-domain="{{domain}}" name="{{bindingName}}" value={{val}} maxlength="{{this.maxLength}}" {{#if this.locked}}readOnly{{/if}}/>
It renders perfect for other values in the for loop,only screws the 1st one with an extra property
I seem to be having trouble with passing the value of an input box to anything else in my javascript.
It's not producing any errors - and I remember reading somewhere that you can have issues if the document hasn't finished loading - but I'm pretty sure it has!
The code in question is as follows in the javascript:
var address = getElementById(addyInput).value;
document.getElementById('add').innerHTML = address;
And in the HTML
<form>
<input name="addyInput" placeholder="Don't forget postcode!">
</form>
<button id="start" onclick="initialize()">Start!</button>
<p>Address Test
<div id="add"></div>
</p>
I know that the button itself is working as it fires the rest of my code fine without the offending code - however the moment I uncomment that little block at the top, it just does nothing. (no errors etc)
Any help on that one would be hot! Thanks :)
Update:
I now have it working! Thanks muchly for all the help!!
Your form needs to look like this (add an id attribute):
<form>
<input id="addyInput" name="addyInput" placeholder="Don't forget postcode!">
</form>
And the first line of Javascript needs to look like this (since getElementById is expecting an ID rather than a name).
var address = getElementById('addyInput').value;
Additionally, getElementById expects the id argument to be a string (hence the quotes). If you pass it addyInput without quotes, it'll try to interpret addyInput as a variable which has a value of undefined and you won't get back the DOM element you want.
Or, if you were using jQuery, you could leave the form markup as-is and change the Javascript to this:
var address = $('input[name=addyInput]').val();
Make sure to specify and id on the input. You only have a name.
You need to add the id "addyInput" to your form input rather than just the name.
getElementById expects a string.
var address = getElementById('addyInput').value;
If you put this directly into a script section in the head, then you will have a problem because the page is not loaded completely but the code is executed already.
And of course you should define an id for the input element as the others already said.
what you are getting is an array, you need to fetch your array into some readable data. Try something like:
$value = array_shift( $yourarray );
or if it's a multi value array you can just loop it to fetch out the values.
I have an asp.net mvc application and i am trying to assign value to my textbox dynamically, but it seems to be not working (I am only testing on IE right now). This is what I have right now..
document.getElementsByName('Tue').Value = tue; (by the way tue is a variable)
I have also tried this variation but it didnt work either.
document.getElementsById('Tue').Value = tue; (by the way tue is a variable)
Can someone where please tell me where I am going wrong with this?
How to address your textbox depends on the HTML-code:
<!-- 1 --><input type="textbox" id="Tue" />
<!-- 2 --><input type="textbox" name="Tue" />
If you use the 'id' attribute:
var textbox = document.getElementById('Tue');
for 'name':
var textbox = document.getElementsByName('Tue')[0]
(Note that getElementsByName() returns all elements with the name as array, therefore we use [0] to access the first one)
Then, use the 'value' attribute:
textbox.value = 'Foobar';
It's document.getElementById, not document.getElementsByID
I'm assuming you have <input id="Tue" ...> somewhere in your markup.
There are two issues in your code.
Use getElementByName instead of getElement**s**ByName
use the value in lowercase instead of Value.
If you are using Chrome, then debug with the console. Press SHIFT+CTRL+j to get the console on screen.
Trust me, it helps a lot.
Sounds like we need to assume that your textbox name and ID are both set to "Tue." If that's the case, try using a lower-case V on .value.
As the plural in getElementsByName() implies, does it always return list of elements that have this name. So when you have an input element with that name:
<input type="text" name="Tue">
And it is the first one with that name, you have to use document.getElementsByName('Tue')[0] to get the first element of the list of elements with this name.
Beside that are properties case sensitive and the correct spelling of the value property is .value.
You can use
formname.textboxname.value="delete";