Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use selected in option attribute in name attribute using Javascript or jQuery?

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.

like image 671
user_v12 Avatar asked Jun 18 '20 06:06

user_v12


People also ask

How do I get the text value of a selected option jQuery?

$var = jQuery("#dropdownid option:selected"). val(); alert ($var); Or to get the text of the option, use text() : $var = jQuery("#dropdownid option:selected").

How do I get the text value of a selected option in JavaScript?

function myNewFunction(element) { var text = element. options[element. selectedIndex]. text; // ... }

How do I select a selected box using jQuery?

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

What is the “option selected” attribute used for?

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.

What does the selected attribute do in HTML?

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.

How to select an element by the name attribute?

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.

How to select an element by name with jQuery?

- 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.


3 Answers

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>
like image 145
DarkBee Avatar answered Oct 11 '22 10:10

DarkBee


Just put this line of code in your script-tag and it will work like you want.

$('#countryselect option[name="1"]').attr('selected',true);
like image 27
Peacycode Avatar answered Oct 11 '22 10:10

Peacycode


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> 
like image 22
Frost Avatar answered Oct 11 '22 09:10

Frost