Brent
Brent

Reputation: 2485

Checking Input Field Empty

I have a button which once pressed will check if a data attribute if is failed or not. If failed it wont send them to the next step.

The problem is if you fail it once you can never get to next step? Any ideas

if ($('#storename').data("search") == "failed"){

   alert("Please select something.");

}else{

  //next step 

 };

Something like this, sorry not used JS fiddle, but this should clear it up. http://jsfiddle.net/gMam4/2/

Upvotes: 0

Views: 75

Answers (3)

Kiran
Kiran

Reputation: 20313

Replace $(".failed").attr("data-search", "failed"); with $('#storename').attr("data-search", "failed"); in your anchor click event. Try this:

 $('#storename').on('keyup',function(e){
      if ($('#storename').attr("data-search") == "failed"){   
       alert("Please select something.");   
    }else{    
       alert("this is not failed");    
     };
 });
   $(".failed" ).click(function() {
      console.log('changing to failed');
      $('#storename').attr("data-search", "failed");
    });

DEMO

Upvotes: 1

Susheel Singh
Susheel Singh

Reputation: 3854

FIDDLE:

http://jsfiddle.net/gMam4/9/

$('#storename').on('keyup',function(e){
      if ($('#storename').data("search") === "failed"){        
       console.log("Please select something.");        
    }else{        
       console.log("this is not failed");        
     };
 });

$(".failed").on('click',function() {
     console.log('changing to failed');
      $("#storename").attr("data-search", "failed");
});

Upvotes: 1

Sharadh
Sharadh

Reputation: 1328

Fix

You might want to get through $('#storename').data('search') and set it through $('#storename').data('search', 'failed'). Mixing up HTML5 data attributes and jquery data attribute seems to be the issue.

Also, please beware of the double equal to == that you've used - might be harmless here but always better to use strict (in)equality with === and !==

Long Version

Using your fiddle from http://jsfiddle.net/gMam4/2/, it looks like you are setting and getting from the 'data-*' attributes. The problem is, that does not directly relate to the $el.data() calls. From the jquery docs:

The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).

Long version here: http://api.jquery.com/data/#data-html5

So, either set the data directly using $el.data('key', value), or use the $el.val(value). In your case, I would think the former...

Upvotes: 0

Related Questions