1

I would like to know if it is possible to add CSS classes to a parent and its child.

For example:

$('#w').html('<ul><li></li></ul>');
$('#w ul').addClass('.filefield-element .filefield-preview');

The desired output:

$('#w').html() is now:
<ul class="filefield-element"><li class="filefield-preview"></li></ul>

Is such a thing possible? Greetings!

2 Answers 2

5

You can try:

$('#w ul').addClass('filefield-element').find('li').addClass('filefield-preview');
Sign up to request clarification or add additional context in comments.

Comments

2

Yes, you just need to select them individually:

$('#w ul').addClass('filefield-element').find('li').addClass('filefield-preview');

In order, that:

  • selects any <ul> elements inside the #w element;
  • adds the filefield-element class to them;
  • selects any <li> elements inside the matched <ul> elements;
  • adds the filefield-preview class to those.

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.