jquery - Why do we write .attr('selected','selected') with select tag

why do we write .attr('selected','selected') with select tag

For ex:

$('#countryList option').filter(function () {
 return ($(this).text() == findText); }).attr('selected','selected');
});

What does it really means?

Explanation of .attr('selected','selected').

First argument inside .attr represent the attribute you want to pointing at while second argument set value of attribute which is passed as first argument.

if we have just .attr('selected') then it just return the value of selected attribute.

.attr(), Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to  .attr() jquery method is used to set the attributes of the selector. So In your case this function is using to show the text as a selected in dropdown. So In your case this function is using to show the text as a selected in dropdown.

.attr() jquery method is used to set the attributes of the selector. So In your case this function is using to show the text as a selected in dropdown.

See tutorial here.

jQuery attr() Method, It contains well written, well thought and well explained computer science and We use cookies to ensure you have the best browsing experience on our website​. The attr() method in jQuery is used to set or return the attributes and values of currentvalue): It is used to specify a function that returns the attribute value to  The .attr() method gets the attribute value for only the first element in the matched set. To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method. Using jQuery's .attr() method to get the value of an element's attribute has two main benefits:

In general, the selected attribute is respected by most browsers by its mere presence/absence irrespective of its value. According to the specification, this is a boolean attribute(when set via javascript), but in HTML markup, this attribute is usually represented by its presence/absence. This means that the element is by default the option visible in a dropdown.

jQuery, Today we will look into following examples – jQuery get attribute, jQuery set attribute I love Open Source technologies and writing about my experience about  jQuery - attr( key, func ) Method - The attr( key, func ) method sets a single property to a computed value, on all matched elements.

why do we write .attr('selected','selected')

That is how jQuery allowed to select, if you want to set the selected item of select you can use val() to set the selected option

$('#countryList').val(5); // it will set the selected attribute for option having value 5

jQuery Get Attribute, Set Attribute, Remove Attribute, In fact, they are known as implied boolean attribute for this very reason. Examples of such attributes are: checked (for checkboxes and radio  What this script does (or should do eventually) is fade in one of the hidden divs (#box1, #box2, etc) depending on what link (see HTML code) is clicked. I added the #showtext div to see of the attribute is being correctly stored. The script works fine, however, only with the first href attribute that is found in the first link.

See what is going on:

$('#countryList option').filter(function () {
   return ($(this).text() == findText); 
}).attr('selected','selected');
}); //<---------this is extra

Okay but question is why do we write .attr('selected','selected') with select tag:

If you can see you are not setting the selected attribute to <select> but <option> it contains.

Your code will set the selected attribute to the option which holds the text which is equivalent to findText

How can you write better in jQuery? - Coding & Design, The jQuery attr() method is used to set or return attributes and values of the selected Convenience: When you use jQuery attr() method to get the value of the  The .attr() call is better when we're dealing with changes on DOM tree that will have a direct influence on the markup, for example if we have to deal with a checkbox where the attribute 'checked' will actually show that checkbox as checked on UI.

jQuery attr(), You can use the jQuery attr() method to add attributes to an HTML element. In the following example when you click on the "Select Checkbox" button it will add  If you want jQuery to get the value of an attribute as a string without any attempt to convert it into other types, you should use jQuery’s attr() method. jQuery only retrieves the value of data

How to Add Attribute to an HTML Element in jQuery, jQuery - Attributes - Some of the most basic components we can manipulate when it comes to DOM elements are the properties and attributes assigned to those  jQuery is very powerful making its slogan very true "Write less do more". Why we should use jQuery. Before reading anything I always raise this question to Google . In the case of jQuery, I am giving the answer- jQuery simplifies JavaScript programming and ensures the code runs on every browser available. We can do most JavaScript code with jQuery.

jQuery - Attributes, Content wise those things are the same, except if you modify. $("#p2").text($('#​txtInput').attr('value') + " - With .attr()");  jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next chapter of this tutorial. Tip: If you don't know CSS, you can read our CSS Tutorial. The Document Ready Event. You might have noticed that all jQuery methods in our examples, are inside a document ready event: $ (document). ready ( function () {

Comments
  • It's more of a convention for boolean attributes; either it's empty or it contains the same value as the attribute name itself.