Select dropdownmenu selected item [duplicate] - javascript

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);

Related

How to store the values of all checked checkboxes with the same class name in an array? [duplicate]

This question already has answers here:
How to retrieve checkboxes values in jQuery
(14 answers)
Closed 9 years ago.
I am looking to get all checkboxes' VALUE which have been selected through jQuery.
You want the :checkbox:checked selector and map to create an array of the values:
var checkedValues = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
If your checkboxes have a shared class it would be faster to use that instead, eg. $('.mycheckboxes:checked'), or for a common name $('input[name="Foo"]:checked')
- Update -
If you don't need IE support then you can now make the map() call more succinct by using an arrow function:
var checkedValues = $('input:checkbox:checked').map((i, el) => el.value).get();

How to use a variable in a Jquery selector [duplicate]

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I want to be able to use a variable inside a Jquery selector:
I have 100 values on screen, called myValue11, myValue21, myValue31 etc, and I want to be able to insert data into one of these values, based on its value
var myVariable;
//Checking if the values are empty, if so, set 'myVariable' to a numeric value
if ($('#myValue11').val() == "")
{
myVariable = 1;
}
else if ($('#myValue21').val() == "")
{
myVariable = 2;
}
else if ($('#myValue31').val() == "")
{
myVariable = 3;
}
//inserting into the first empty value, by using 'myVariable'
$('#myValue+myVariable+1').val("Hello World!"); //Doesn't work
The code above doesn't work, I can't seem to find a way to insert this variable into the selector, any help would be appreciated. Thank you
Try
$('#myValue'+myVariable+'1').val("Hello World!");
You needed the extra quotes to terminate the strings.

Use value of id to search for class [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 4 years ago.
Im trying to get the value of an html element via Id. I now want to use that value to loop through all the elements with the class from the id.val. How can I search for a variable as a class.
$(".BtnSettings").click(function () {
var Daytime = $("#Daytime").val();
//Here i dont know what to do. Instead of .Daytime I want to search for the var Daytime. Or for $("#Daytime").val() obviously, doesnt matter.
$('.Daytime').each(function () {
$(this).prop("checked", true); // Element(s) are now enabled.
});
})
you can use the attr method of jquery to look for a specific attribute just set a value. if you want to change value of said attribute the second parameter of the function will set it for you like.
Daytime.attr("class","newClass")
$("#Daytime").attr("class")
will give you the class of #Daytime

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;

Loop through Drop Down Option and read its attribute using jquery [duplicate]

This question already has answers here:
How to get the attributes of a HTML element using JQuery?
(4 answers)
Closed 8 years ago.
I am looping through drop down option and checking attribute.
if attribute match than counter is increase. At the end i show counter as alert.
This is my code but some how its not working dont know why
var count= 0;
$('.mydropdown option').each(function () {
var level = this.attr("myattr");
if (level == "0") {
count++;
}
});
alert(count);
}
this is a plain javascript object, it does not contain a function called .attr()
Try,
var level = $(this).attr("myattr");
Just convert the this reference into a jquery object and invoke .attr() over it

Categories

Resources