0

I have a huge checkbox list with different levels and I would like to style it but since all the items have the same class and no id or contact form 7 for wordpress has no option to make sublevels in the checkbox panel I thought I could achieve this with jquery.

The html generated is:

<span class="wpcf7-list-item-label">--- Checkbox level one</span>
<span class="wpcf7-list-item-label">------ Checkbox level two</span>

What I am trying to achieve is to replace

<span class="wpcf7-list-item-label">--- 

with

<span class="wpcf7-list-item-label level-one">

I tried with this code

 <script>
    $("<span class=\"wpcf7-list-item-label\">---").replaceWith( "<span class=\"wpcf7-list-item-label level-one\">" );
    </script>

But with no success, is it possible to achieve this with jquery or not ?

Thanks for your time.

1
  • 2
    so you only need to add one class? use the addClass() method... Commented Jun 18, 2012 at 8:37

3 Answers 3

4
$('span.wpcf7-list-item-label:first').addClass("level-one")

If you want to add a class to all spans based on their index:

$('span.wpcf7-list-item-label').each(function(index){
    $(this).addClass('level-' + index);
});
Sign up to request clarification or add additional context in comments.

4 Comments

What about the three hypens? Hint: .text()
Thanks but maybe I didn't explain well. I have a lot of checkboxes, so for example I have that code 10 times, then I have another with ------ instead of --- 30 times, then again --- then 20 more starting with ------, so I need to add class based on what the checkbox label content is starting with --- or ------, not just the first.
@JohnSmith. see the update, I thought this might be the case.
Thanks gdoron, Not exactly what I was looking for but it will get the job done.
2

Use this :

$('.wpcf7-list-item-label').first().addClass('level-one');

For checking particular text,

$(".wpcf7-list-item-label:contains('---')").addClass('level-one');

Comments

1

Use this:

$('.wpcf7-list-item-label').first().removeClass("existingclass").addClass("newclass");

3 Comments

.removeClass("existingclass") why is that for?
Removing for existing one as the name suggests, then adding new one
But he's not trying to remove one in the question. :)

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.