1

From the example below how can I remove the 2nd class using jquery?

From this:

<input type="text" class="span {validate:{required:true, messages:{required:'Please enter name'}}}" id="lname" name="lname">

To this:

<input type="text" class="span" id="lname" name="lname">

NOTE:

  1. Element can have several classes
  2. {validate: ...} is created dynamically
1
  • 3
    Why you don't use a data attribute for this information? like <input type="text" class="span" data-validation="{validate:{required:true, messages:{required:'Please enter name'}}}" id="lname" name="lname"> Commented Oct 20, 2012 at 19:45

4 Answers 4

1

If you just want to keep the "span" class, you could do something like:

$('.span').attr('class', 'span')

If you want to remove the second class, whatever its name is, you could do something like:

$('.span').each(function() {
    $(this).attr('class', $(this).attr('class').replace(/^(.+?) [^ ]+/, '$1'))
});

Note that a class name cannot have spaces in it, therefore the second class should be the class following the first space and ending either at the next space or the end of the attribute value.

Sign up to request clarification or add additional context in comments.

Comments

0
$('.span').removeClass('{validate:{required:true, messages:{required:'Please enter name'}}}');

Comments

0

Well, generally you would do this..

$("#element").removeClass("nameOfClass");

But this is some strange class, you've got there..

Comments

0

Use remove class

$('input, select, textarea', '#form_id').removeClass('errorClass');

Comments

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.