How's Jquery to assign the sss a attribute?



  • Got a list of elements.

    <li>
        <label  data-bg="#d6eff3" ></label>
    </li>
    <li>
        <label  data-bg="#fff" ></label>
    </li>
    <li>
        <label  data-bg="#ff0000" ></label>
    </li>
    

    How to make their background up straight from the atribut. data-bg?



  • If I' correctly understood, then that way:

    $('#list li').each(function() {
      $(this).css("background-color", $(this).data('bg'));
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <ul id="list">
      <li data-bg="blue">1</li>
      <li data-bg="red">2</li>
      <li data-bg="green">3</li>
    </ul>

    We're going through all the items on the list. http://api.jquery.com/jquery.each/ ♪ we're making meaning ♪ data-bg And we're stylish.

    Updated
    You have edited the question, new moments have opened, but the principle does not change. You're the only one who has to choose. labeland if it's necessary to give background to the current liI'll use it. https://api.jquery.com/parent/

    $('#list li label').each(function() {
      $(this).parent().css("background-color", $(this).data('bg'));
    })
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2