0

I have several forms in one page, and I wanted to target all input fields in a target form (form ID) that has a certain class in it (Eg."has-error" ).

I though this would do the trick:

target_elem = "#form_b";
$(target_elem + ":input").hasClass("has-error").removeClass("has-error");

No luck so far. I've tried playing w/ filtering as well. tsk

1
  • 1
    What do you think #form_b:input will select? And what will hasClass return? Did you read the documentation at all? api.jquery.com/hasclass Commented Jun 17, 2014 at 7:49

4 Answers 4

3

Demo

Simply Use .class selector:

$(target_elem + " input.has-error").removeClass("has-error");
Sign up to request clarification or add additional context in comments.

4 Comments

The best ; even if I'm don't know if the class selector is really useful. We could apply removeClass to all inputs in the target element also... Wonder which solution is the fastest.
Instead of finding/searching for all inputs in the DOM this would only search for those input with the given class
Yes but the target_elem is already an ID, so the search won't by in the entire DOM. For a small number of input to update, I should be faster not to search by class.
This was a very elegant solution. thanks pete. and thanks all for participating.
0

Try this , Let me know if it helps :

  $(".has-error").each(function(){
    $(this).removeClass("has-error");


  });

Live example : http://jsbin.com/yudaseqe/1/edit

Comments

-1

No need to use ':' $(target_elem + " input").hasClass("has-error").removeClass("has-error"); should work

Note that you also can forget about hasClass("has-error") It will take more time to find input with this class than deleting this class of every input without checking if it exists.

target_elem = "#form_b";
$(target_elem + " input").removeClass("has-error");

Comments

-1

try this...

$("input[class*='has-error']",$(target_elem)).removeClass("has-error");

Or use this

$("input.has-error",$(target_elem)).removeClass("has-error");

1 Comment

Performances of *= are really bad compared to the class selector !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.