Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. How to give unique ids to changing divs?

How to give unique ids to changing divs?

Scheduled Pinned Locked Moved JavaScript
questionhtmldockerdata-structurestutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Rathore
    wrote on last edited by
    #1

    The function displays values of options property of array in separate divs. I want to give each div a unique id, which I have given, but the first displayed divs are replaced by totally different divs on click; so I am unable to give ids to each div. I want that divs should remain same and only the values should change on click. Presently the ids that I have given are removed when next values are displayed on click. How can I achieve that? Thanks for your suggestions.

    Options 
    

    var antonyms = [

        {
            question: "soft",
            options: \["dark", "white", "heavy", "hard", "down", "pretty", "butter", "cotton"\],
            answer: "hard"
    
        },
    
        {
            question: "beautiful",
            options: \["pretty", "ugly", "handsome", "wonderful", "up", "high", "cheerful", "black"\],
            answer: "ugly"
    
        },
     \]
    
    // Define a method that generates the HTML for one question.
    
    
    function showOptions(q)
    
    {
        let qHtml = '
    

    ' + q.question + '

    ';

        qHtml += q.options.map(o => '
    

    ' + o + '

    ').join("");

        return qHtml;
    
    }
    

    ```
    // Start showing the first question. Click event will show next question.

    let container = document.getElementById("container");
    
    let button = document.getElementById("btnNext");
    
    let qIdx = 0;
    
    container.innerHTML = showOptions(antonyms\[qIdx\]);
    
    button.addEventListener("click", function()
    
        {
    
            qIdx += 1;
    
            if (qIdx < antonyms.length)
    
                container.innerHTML = showOptions(antonyms\[qIdx\]);
    
            else
    
                button.disabled = true;
    
        });
    

    ```
    // Giving unique id to each option div

    $('#container div').each(function(eq, el) {
    
        el = $(el);
    
        if (typeof(el.attr('id')) === "undefined") {
    
            el.attr('id', 'box-' + eq);
    
        }
    
    });
    
    Z 1 Reply Last reply
    0
    • T Tarun Rathore

      The function displays values of options property of array in separate divs. I want to give each div a unique id, which I have given, but the first displayed divs are replaced by totally different divs on click; so I am unable to give ids to each div. I want that divs should remain same and only the values should change on click. Presently the ids that I have given are removed when next values are displayed on click. How can I achieve that? Thanks for your suggestions.

      Options 
      

      var antonyms = [

          {
              question: "soft",
              options: \["dark", "white", "heavy", "hard", "down", "pretty", "butter", "cotton"\],
              answer: "hard"
      
          },
      
          {
              question: "beautiful",
              options: \["pretty", "ugly", "handsome", "wonderful", "up", "high", "cheerful", "black"\],
              answer: "ugly"
      
          },
       \]
      
      // Define a method that generates the HTML for one question.
      
      
      function showOptions(q)
      
      {
          let qHtml = '
      

      ' + q.question + '

      ';

          qHtml += q.options.map(o => '
      

      ' + o + '

      ').join("");

          return qHtml;
      
      }
      

      ```
      // Start showing the first question. Click event will show next question.

      let container = document.getElementById("container");
      
      let button = document.getElementById("btnNext");
      
      let qIdx = 0;
      
      container.innerHTML = showOptions(antonyms\[qIdx\]);
      
      button.addEventListener("click", function()
      
          {
      
              qIdx += 1;
      
              if (qIdx < antonyms.length)
      
                  container.innerHTML = showOptions(antonyms\[qIdx\]);
      
              else
      
                  button.disabled = true;
      
          });
      

      ```
      // Giving unique id to each option div

      $('#container div').each(function(eq, el) {
      
          el = $(el);
      
          if (typeof(el.attr('id')) === "undefined") {
      
              el.attr('id', 'box-' + eq);
      
          }
      
      });
      
      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      I don't quite follow where you are lost, but if you want the id to be unique then keep a global variable counter and add it to the end of each div's id and then increment it.

      Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups