PSR
PSR

Reputation: 40318

How I can use a class name here in jQuery

I want to use this for labels which contains myClass

if ($(field).val() != ""){
  var fieldId = $(field).attr("id");
  $("label[for='"+fieldId+"']").hide();
}

The above code is useful to put ID, but how I can use this for classes?

Upvotes: 1

Views: 50

Answers (1)

George Reith
George Reith

Reputation: 13476

  var myClass = "yourclass";
  $("label." + myClass).hide();

The jQuery selector $() takes strings equivalent to CSS1-3 selectors (plus a few others). So if you can select it via CSS you can select it via jQuery.

Upvotes: 1

Related Questions