1

I want to know that there are how many ways in JS to create sub tags and their classes names from a given array eg array =['one','two','three','four','five']. If I be more specific I have already created main tags ie <li class="card"></li>............<li class="card"></li> of class deck through JS

`let displayCards = document.querySelector('.deck');
 for (i=0; i < symbols.length; i++) {
    let cards = document.createElement('li');
    cards.classList.add('card');
    displayCards.appendChild(cards);
 }`

this is the end result I want:

<ul class="deck">
    <li class="card">
        <i class="one"></i>
    </li>
    <li class="card">
        <i class="two"></i>
    </li>
    <li class="card">
        <i class="three"></i>
    </li>
</ul>

SO...How many ways to create sub-tags and sub-classes of all class card of above mentioned array. Many Thanks

4
  • I didn't understand what is the end result you want? please provide what do you really need Commented Jul 13, 2018 at 13:15
  • this is the end result that I want. Thanks <ul class="deck"> <li class="card"> <i class="one"></i> </li> <li class="card"> <i class="two"></i> </li> <li class="card"> <i class="three"></i> </li> ..................... /ul> Commented Jul 13, 2018 at 13:28
  • Hey sorry for the delay, I just posted my answer, have a look and let me know if it works. Commented Jul 16, 2018 at 6:21
  • Hi Zeeshan...I will check it later on.....Thanks Commented Jul 16, 2018 at 13:46

1 Answer 1

1

Here's what you can do it's just like what you tried already.

Html:

 <ul class="deck">
 </ul>

JS:

             <script>
              $(document).ready(function () {
                    var symbols = ['one', 'two', 'three', 'four', 'five']
                    let displayCards = document.querySelector('.deck');
                    for (i = 0; i < symbols.length; i++) {
                          let cards = document.createElement('li');
                          cards.classList.add('card');
                          let childCard = document.createElement('i');
                          childCard.classList.add(symbols[i]);
                          childCard.innerHTML += symbols[i];
                          cards.appendChild(childCard);
                          displayCards.appendChild(cards);
                    }
              });

        </script>
Sign up to request clarification or add additional context in comments.

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.