onChange select list [duplicate] - javascript

This question already has answers here:
What is console.log?
(26 answers)
Closed 8 years ago.
<form name="storelocator_cities_form" id="storelocator_cities_form">
<select name="storelocator_city" id="storelocator_city" onchange="storeLocatorForm.fetchStoresByCountryAndPostCode(this)">
<option value=""></option>
</select>
</form>
Hi i want to capture the option value and then compare it to a specific city and i'm stuck. I'am new with both JS and jQuery but i guess that i will need to do something like
jQuery(function(){
$('#storelocator_city').on('change',function(){
var city = $(this).val();
});
});
I would like a way to see that i get the correct value in the var in the console.

I would like a way to see that i get the correct value in the var in
the console
Sure , but first :
Why do you have 2 onchange events ?
Anyway - to see the value in console : try this :
jQuery(function(){
$('#storelocator_city').on('change',function(){
var city = $(this).val();
console.log(city); <------- Try this
});
});

Related

Datatype .innerHTML [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 2 years ago.
I´m getting a NaN out of this script, is it a data type problem and how do i solve it?
<script>
function calc(){
var profile = document.getElementById("profil");
var hours = document.getElementById("hour");
document.getElementById("result").innerHTML=profile*hours;
}
</script>
Present the result like this
<button onclick="calc()">Calc</button><br>
<p id="result"></p>
You have to get the value from the element.
var profile = document.getElementById("profil").value;
With .getElementById you get a DOM Element.
Supposing "profil" and "hour" are input elements, you want to obtain their values:
<script>
function calc(){
var profile = document.getElementById("profil").value;
var hours = document.getElementById("hour").value;
document.getElementById("result").innerHTML=profile*hours;
}
</script>

reading selected value from dropdown [duplicate]

This question already has answers here:
Get selected value in dropdown list using JavaScript
(32 answers)
Closed 5 years ago.
i am trying to read selected value from dropdown list( ddDocTypes) . So in the IE I am able to get value using Form1.ddDocTypes.options(Form1.ddDocTypes.selectedIndex).innerText. But the same code give me "Form1.ddDocTypes.options undefined" error in Chrome.
Any suggestions !!
I don't know if you are just trying to get the value of the selected from the dropdown, but this is how you do it in plain javascript (guessing from your tag)
var e = document.getElementById("ddDocTypes");
var inner = e.options[e.selectedIndex].text;
var value = e.options[e.selectedIndex].value;

how to get value by class name using javascript [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 7 years ago.
Sorry, it's basic one but I trying to search on google anyways but still not get success.
I want get value of this
<input type='hidden' class='hid_id' value='1' />
Using Javascript I want alert value 1
I trying this
var id = document.getElementsByClassName("hid_id");
alert (id);
But it's alert [object HTMLInputElement]
please help me now.
getElementsByClassName() returns an array so you have to access first element (if there is any). Then try accessing value property:
var id = document.getElementsByClassName("hid_id");
if (id.length > 0) {
alert (id[0].value);
}
jsfiddle
try this:
var id = document.getElementsByClassName("hid_id")[0].value;

jQuery cannot get input value [duplicate]

This question already has answers here:
How to select JSF components using jQuery?
(4 answers)
Closed 7 years ago.
I am trying to get input value after keypress by jQuery. I have tried few ways to get it and I receive undefined or "". Using JSF+RichFaces. Input is the <h:inputTextarea> Trying to get value by:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery("myform:idTextarea").val();
console.log(testValue);
});
});
</script>
The variable in log is undefined.
When I add "input:" like that
var testValue = jQuery("input:myform:idTextarea").val();
I get j_id12.
When change like that:
var testValue = jQuery("#myform:idTextarea").val();
I get "".
Have someone any clues?
You need to escape CSS meta-character while buidling the jquery selector:
jQuery("input\\:myform\\:idTextarea").val();
Escaping css meta character in jquery
You don't need to find same element for which keyup is registered, just use this
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#myform:idTextarea").keyup(function(){
var testValue = jQuery(this).val();
console.log(testValue);
});
});
</script>

Select dropdownmenu selected item [duplicate]

This question already has answers here:
How can I set the value of a DropDownList using jQuery?
(17 answers)
Closed 8 years ago.
I was wondering if anyone could advise me a little. I am attempting to use JQuery to select my dropdown item by a given value. Here is what I have
var myVar = document.getElementById('<%= hiddenID.ClientID %>').value
if (myVar != "") {
var val = $("select[name*=drpDescription] option").filter(function () {
return ($(this).val() == myVar)
});
$("select[name*=drpDescription]").val(val.val());
}
The above code snippet does get the correct option based on it's value matching my hiddenfield value. What I can't do is get my dropdown to display it as the selected item. I wondered if anoyone has any advise on where I am slightly missing the point.
Thank you for any help.
You can set the value by passing it as parameter in .val() with select elements selector:
$("select[name*=drpDescription]").val(myVar);

Categories

Resources