If you want to study these concepts in depth, take a look at MDN. 1- val () – method returns the value of selected attribute value. Syntax. How to get attribute from selected option using jQuery, How to create text editor in textarea using jquery, How to converting pacific time string to time format in PHP, Restrict characters in input field using javascript, Read text file draw line between two points google map API using javascript, Pick up Location and Drop Location line draw in google map API using javascript, How to Subscribe to List using MailChimp API using PHP, How to Create Dynamic Excel File Using PHPExcel Library in Codeigniter, How to create flash messages in Codeigniter. To get the value for each element individually, use a looping construct such as jQuery's.each () or.map () method. $(document).ready(function(e) { $('#userForm').on("submit",function(){ var city = $('#city option:selected').data(); console.log(city); var state = $('#state option:selected').attr('data-state'); console.log(state); }); }); Output: {city: 3} 2 The jQuery attr() method is used to get or set attributes and values of the selected html elements. We will also see various operations that can be performed over it with the below given examples. To get value of first option. The HTML To achieve the best performance when using :selected to select elements, first select the elements using a pure CSS selector, then use .filter(":selected"). Add attribute To add an attribute to a HTML element /or elements (any attribute, id, class, href, selected, etc. The option that is having the selected attribute will be displayed by default. Home » Jquery » jQuery – getting custom attribute from selected option jQuery – getting custom attribute from selected option Posted by: admin November 5, 2017 Leave a comment Copyright © 2021 Tutorial Republic. All Rights Reserved. However, simply using the $.text will return the whole text of the select dropdown. The code grabs the option element for Indonesia, clones it and puts it into a new div (not in the document) to retrieve the full HTML string: . jQuery Get Data Attribute Method jQuery offers various method to get data attribute values, Here You can learn two simple method to get data-any attribute of selected html Elements. For example, using the following select box: . jquery get data attribute of selected option . How to Get Selected Radio Button Value. Select every element containing an id attribute with the value "choose": $("[id=choose]") If inside a change handler, you could use simply this.value to get the selected option value. Removing an item from a select box using jQuery. You have to pass attribute as an argument of the attr () which you want to get. You can use the CSS attribute selectors to find an HTML element based on its data-attribute value using jQuery. You can also get select option attribute values with the help of JQuery for that use below code. The name attribute used here to group the radio buttons. A multiple select box allows a user to select multiple options. $ ( document ) .ready ( function (){ $ (' #dropdownList ').on( 'change' , function (){ //var optionValue = $(this).val(); //var optionText = $('#dropdownList option[value="'+optionValue+'"]').text(); var optionText = $ (" #dropdownList option :selected") .text (); alert ( "Selected Option Text: " + … Suppose you have an anchor tag and you want to get the href attribute of it. See demo for more options. You can use the jquery removeAttr () attribute method to remove the selected HTML elements attribute or remove disabled attribute jquery. Let's try out the following example to understand how it basically works: If the value for an option is not defined specifically, the text content of the