I want to use “selected” in option attribute in name attribute using Javascript or jQuery. It means I want to use name="1"
option as selected
one when the page is loaded. I tried with the below code. But it not workes.
Code:
<select id="countryselect" name="country">
<option value="1" name="0">Afghanistan</option>
<option value="2" name="0">Albania</option>
<option value="3" name="0">Algeria</option>
<option value="4" name="1">Malaysia</option>
<option value="5" name="0">Maldives</option>
</select>
Jquery:
$("#countryselect").$('option[name="1"]')
Demo: http://jsfiddle.net/muthkum/zYRkC/
Thank you.
$var = jQuery("#dropdownid option:selected"). val(); alert ($var); Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected").
function myNewFunction(element) { var text = element. options[element. selectedIndex]. text; // ... }
You can select on any attribute and its value by using the attribute selector [attributename=optionalvalue] , so in your case you can select the option and set the selected attribute. $("div. id_100 > select > option[value=" + value + "]"). prop("selected",true);
The “option: selected” attribute is used to select specific content in the option tag. The selector is the id name (#IdName) of the select tag. The basic syntax used in the web page is below.
The selectedattribute is a boolean attribute, its presence sets the value of the related DOM property to true. If the attribute is absent, the value of the selected property is false. If an option has the selected attribute, then when the page is first loaded, or the form the control is in is reset, that option will be the selected option.
An element can be selected by the name attribute using 2 methods: The name attribute selector can be used to select an element by its name. This selector selects elements that have the value exactly equal to the specified value.
- GeeksforGeeks How to select an element by name with jQuery ? In this article, we will learn to get the selected element by name in jQuery. An element can be selected by the name attribute using 2 methods: We will understand both methods with the help of examples. The name attribute selector can be used to select an element by its name.
You can pass a second parameter to the selector to define the scope
$('#countryselect').val($('option[name="1"]', "#countryselect").val());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="countryselect" name="country">
<option value="1">Afghanistan</option>
<option value="2" name="1">Albania</option>
<option value="3">Algeria</option>
<option value="4">Malaysia</option>
<option value="5">Maldives</option>
</select>
Just put this line of code in your script-tag and it will work like you want.
$('#countryselect option[name="1"]').attr('selected',true);
If you want to go with dynamic assignment, you can go with
$("#countryselect").val(1) .
If selection of option is static and only want for initial load, then selected can be used for this like
<option value="4" selected>Malaysia</option>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With