Skip to content Skip to sidebar Skip to footer

Compare Class Name With Variable In Javascript

I have input element with class names like what I want to do is if a variable matches a particular class name then do something $(function(){ var

Solution 1:

You can use .hasClass():

$(function() {
  var x = "os";
  var y = $("input").hasClass(x);
  if (y) {
    $('input').val('foobar!!!').css('color', 'red');
  }
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputclass="so os us">

Solution 2:

You can use jQuery's hasClass function.

https://api.jquery.com/hasclass/

Post a Comment for "Compare Class Name With Variable In Javascript"