jQuery get value of select onChange

0 votes

I was under the impression that I could get the value of a select input by using this:- $(this).val();

and applying the onchange parameter to the select field.

It would appear it only works if I reference the ID. How could I do this using this? 

Feb 11, 2022 in Java by Rahul
• 9,680 points
21,866 views

1 answer to this question.

0 votes

Try the following lines of code-

$('select').on('change', function() { 
    alert( this.value ); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script> <select> 
            <option value="1">One</option> 
          <option value="2">Two</option> 
</select>

You can also reference with onchange event-

function getval(sel) 
{ 
      alert(sel.value); 
}
<select onchange="getval(this);"> 
      <option value="1">One</option> 
      <option value="2">Two</option> 
</select>

answered Feb 11, 2022 by Soham
• 9,710 points

Related Questions In Java

0 votes
1 answer

Get selected text from a drop-down list (select box) using jQuery

Select elements typically have two values that ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
3,203 views
0 votes
1 answer

How to set selected value of jQuery Select2?

The third parameter of the new Option ...READ MORE

answered May 30, 2022 in Java by gaurav
• 23,260 points
40,356 views
0 votes
1 answer

how to get ascii value of char in java

In Java, you can get the ASCII ...READ MORE

answered Nov 24, 2023 in Java by Zoya
387 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,905 views
0 votes
1 answer

jQuery get value of select onChange

Try this- $('select').on('change', function() { alert( this.value ...READ MORE

answered Jun 23, 2022 in Web Development by rajatha
• 7,680 points
574 views
0 votes
1 answer

jQuery get value of select onChange

To get the value and text of ...READ MORE

answered Jun 23, 2022 in Web Development by rajatha
• 7,680 points
1,106 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
741 views
0 votes
1 answer

Is 'sparkline' a method?

I suggest you to check 2 things That jquery.sparkline.js is actually ...READ MORE

answered Nov 9, 2018 in Apache Spark by Frankie
• 9,830 points
1,262 views
0 votes
1 answer

jQuery AJAX submit form

There is a simple input mentioned below ...READ MORE

answered Feb 8, 2022 in Java by Soham
• 9,710 points
1,640 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP