views:

645

answers:

2
+1  Q: 

Using If in JQuery

I'm using the jQuery Clean Calendar Plugin, and it's working well. However, in my jQuery code, I want to check whether any '/' is present in textbox.val(). Then I want to do some operations. How do I check if the value contains '/' in it?

+2  A: 

Try

   if (textbox.val().indexOf( "/") >= 0)
   {
   }

Same I guess will work for attribute:

   if ($( "#id").attr( "some_attr").indexOf( "/") >= 0)
   {
   }
Artem Barger
+1  A: 

Just to suggest a slightly more concise way to do it, using ternary and match() instead:

$( "#id").attr( "some_attr").match('/') ? alert('got a forward slash') : alert('no forward slash');
karim79
I love the ternary operator, but I'm thinking it may confuse the original poster. He seems relatively new to programming, and the if/then structure might make more sense than the () ? : ; structure.
Jonathan Sampson
@Jonathan - I totally agree with you. I just felt like suggesting it as no-one else had, and to introduce a bit of variety.
karim79