mohamedzajith
mohamedzajith

Reputation: 397

get the id value before the function by using jquery

$(document).ready(function(){
    $("#test").validate();

    function validate(){
       var x= //do some code it will return #test
       alert(x);
    }
});

I want to the value test or #test how to get it how can do that?

Upvotes: 0

Views: 61

Answers (1)

punkbit
punkbit

Reputation: 7707

// first, extend jquery with `validate` method
$.fn.validate = function() {
  var isValid = this.attr('id') === 'test'
  return isValid
}

    
$(document).ready(function(){    
  if ($("#test2").validate()) {
    console.log('#test is valid')
  }

  if($(".other").validate()) {
    console.log('#other is valid')  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="test2">bla bla bla<div>
<div class="other" id="test">bla bla<div>

Upvotes: 1

Related Questions