Avatar of Stephen Forlance
Stephen Forlance
 asked on

Getting selected value using Jquery

Hi all, I have this simple form which triggers a jquery function when a change happens, but how can I get the actual selectedindex value?


<html>
	<head>
	<script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>
	<script>
$(document).ready(function() {
  $('#form select').change(function() {

    //var strChosen = $(this).attr('id');
    var strChosen = $(this).attr('id');
   
    alert(strChosen);
  });
});
		</script>	
	</head>
	
	
	<body>
		
		<form id="form">
			
<div><p>			Question 1: Name</p>
<input type="textfield" name="name" id="name">
</div>

<div> Question 2: Pet Type
	
	<select id="pet">
		<option value=""></option>
		<option value="Dog">Dog</option>
		<option value="Cat">Cat</option>
	</select>
	
</div>


<div> Question 3: Dog Breed
	
	<select id="breed" name="breed">
				<option value=""></option>
		<option value="Bernese">Bernese</option>
		<option value="American Cocker Spaniel">American Cocker Spaniel</option>
	</select>
</div>

<div> Question 4: Cat Breed
	
	<select id="catbreed">
				<option value=""></option>
		<option value="Black and White">Black and White</option>
		<option value="Tabby">Tabby</option>
	</select>
</div>
		</form>
	</body>
</html>

Open in new window

jQueryJavaScript

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Julian Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Stephen Forlance

ASKER
Thank you!
Julian Hansen

You are welcome.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy