Solved

jQuery - Problems checking text for acquired "selected" drop down option.

Posted on 2008-10-17
1
1,800 Views
Last Modified: 2011-10-19
I have managed to grab the text of a selected option from a drop down, but when I assign that value to a variable and run it through a conditional statement it doesn't work.  I run a "alert ( variable )" to see what value was assigned to the variable and see that it does obtain one of the conditions "true".  But an odd thing is that the "alert" window puts the text all the way to the right (SEE ATTACHED FILE) of a screen shot of what I mean.

Details:
- Drop down with: class="choice"
- Drop down contains to options: "Someone Else" and "Me" as your two choices
- By default the drop down has "Me" selected. I need to see when the drop down changes (.change(function()) if "Someone Else" was selected.

NOTE: I have an alert ( ) there to return what is in the variable so I can see myself and it always returns blank.

Here is my attempted code:

<script type="text/javascript">
jQuery.noConflict();
   jQuery(document).ready(function(){
      jQuery(".optional-field").hide();
      jQuery(".warning").hide();
          
  jQuery(".choice").change(function() {
   var choice = jQuery(".choice option:selected").text();
   alert (choice);
 
      if (choice == 'Me') {
         jQuery(".optional-field").hide();
         jQuery(".warning").hide('slow');
      } else if (choice == 'Someone Else') {
         jQuery(".optional-field").show('slow');
         jQuery(".warning").show('slow');
      }
   });
});
 
</script>

Open in new window

0
Comment
Question by:pghtech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
sh0e earned 125 total points
ID: 22743286

<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
jQuery.noConflict();
   jQuery(document).ready(function(){
      jQuery(".optional-field").hide();
      jQuery(".warning").hide();
          
  jQuery(".choice").change(function() {
   var choice = jQuery(".choice option:selected").text();
   alert (choice);
 
      if (choice == 'Me') {
         jQuery(".optional-field").hide();
         jQuery(".warning").hide('slow');
      } else if (choice == 'Someone Else') {
         jQuery(".optional-field").show('slow');
         jQuery(".warning").show('slow');
      }
   });
});
 
</script>
<form>
<select class="choice">
<option value="volvo">Me</option>
<option value="saab">Someone Else</option>
</select>
</form>
<div class="optional-field">optional-field</div>
<div class="warning">warning</div>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery dialog box help 2 23
IFrame in MVC 2 35
Is there a way to bind the ID to the text value of the search result? 28 68
Jquery Bold 3 specific words in a paragraph 5 24
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question