Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

    Creating Unique Class Names when Creating/Cloning New Elements Using Javascript (jQuery)

    Javascript + jQuery
    2
    3
    27
    Loading More Posts
    • 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.
    • A
      Abdullah last edited by avan

      *****Hello i am curently working on how to clone a div and then assign new classes to it , it works actually but everytime the clone div is made they all have same new classes, how can make them all have different classes thanks


      $(".postingContent").on("click", function () {
            //function to use to clone;
              var newItem = function () {
                  var cloneCount = 0;
                  cloneCount++;
                  return $('.UserContentLi')
                      .clone(true, true)
                      .attr({'class': 'clon_' + cloneCount++}).insertAfter($('[class^=UserContentLi]:last'))  
                      .appendTo(".wholeUserContentLayer")
                       //changing classes for output text user wrote
                      .find(".textsUserWroteOnline").attr({ 'class': "textsUserWroteOnlineNew" + cloneCount++ }).addClass("textsUserWroteOnlineNewClass");
              };
              //cloning element i want
               newItem($(".UserContentLi").each(function () {
                  $(".textsUserWroteOnline").html($(".bioInput").val());
               
              })); 
             
              
      
          });
      
      Reply Quote 1
        1 Reply Last reply

      • avan
        avan last edited by

        @Abdullah

        One thing that stands out is that you are resetting cloneCount every-time newItem is called. So move 'var cloneCount = 0;' to the top of the javascript file? That is just one option.

        Reply Quote 1
          A 1 Reply Last reply

        • avan
          avan last edited by

          @Abdullah

          One thing that stands out is that you are resetting cloneCount every-time newItem is called. So move 'var cloneCount = 0;' to the top of the javascript file? That is just one option.

          Reply Quote 1
            A 1 Reply Last reply

          • A
            Abdullah last edited by @avan

            @avan Thaaaaaaaaaaaaaaaanks very much, i freaking appreciate this website,always learning ,it worked thanks 😀

            Reply Quote 1
              1 Reply Last reply

            • First post
              Last post