views:

346

answers:

2

I have a shoppingcart that displays product options in a dropdown menu, and I want to make some other fields on the page only visible if they select "Yes" in the previous option. The problem is that the shopping cart also includes the price modifier in the text, and that can be different for each product. So if I do this it works:

 $(document).ready(function() {
  $('select[id="Engraving"]').change(function() {
   var str = $('select[id="Engraving"] option:selected').text(); 
   if (str == "Yes (+ $6.95)") {
    $('.engraving').show();
   } else {
    $('.engraving').hide();
   }
  });
 });

However I would rather use something like this:

 $(document).ready(function() {
  $('select[id="Engraving"]').change(function() {
   var str = $('select[id="Engraving"] option:selected').text(); 
   if (str *= "Yes") {
    $('.engraving').show();
   } else {
    $('.engraving').hide();
   }
  });
 });

Which doesn't work.

I only want to perform the action if the selected option contains the word "Yes", and would ignore the price modifier.

I appreciate any help.

+3  A: 

Like this:

if (str.indexOf("Yes") >= 0)

Note that this is case-sensitive.
If you want a case-insensitive search, you can write

if (str.toLowerCase().indexOf("yes") >= 0)

Or,

if (/yes/i.test(str))
SLaks
Wow, that was fast. Worked Perfect. Thank you.
Jordan Garis
A: 

You could use search or match for this.

str.search( 'Yes' )

will return the position of the match, or -1 if it isn't found.

hookedonwinter