3

I have this template div

<div class="divDetails">
    <div class="divRowData" style="display: none "> 
          <span class="spanColor"></span>
          <span class="spanText"></span>
    </div>
</div>

It is hidden.

I have an object array and I need to duplicate divRowData with the injected data from array. ( and make each new section visible ).

But it seems my jquery code is a bit of a mess and I'm looking for a shorter way of doing it.

Currently I do :

 for (var i = 0; i < arrData.length; i++)
           {
               var thediv = $(".divDetails>.divRowData:first").clone().css('display','block');
               var g = thediv.find(".spanColor").css('background-color', 'color'+i);
               var g = thediv.find(".spanText").text(arrData[i].text);
               thediv.appendTo(".divDetails");

           }

How can I shorten this code ?

http://jsbin.com/etudak/1/edit

I know I can use templates but that's another story.

2 Answers 2

3
$.each(arrData, function(i, v){

    $(".divDetails>.divRowData:first")
     .clone()
     .show()
     .find(".spanColor").css('background-color', 'color'+i)
     .end()
     .find(".spanText").text(v.text)
     .end()
     .appendTo(".divDetails");

});

Since some traversing is necessary I couldn't get it shorter than this. But at least it's a chained example ;)

Sign up to request clarification or add additional context in comments.

2 Comments

They both seems to work ....and I couldnt understand the differnce between them ...:-(
@RoyiNamir "As described in the discussion for .end(), jQuery objects maintain an internal stack that keeps track of changes to the matched set of elements. When one of the DOM traversal methods is called, the new set of elements is pushed onto the stack. If the previous set of elements is desired as well, .andSelf() can help."
1

Perhaps, you can't shorten the code all that much, if you don't want to use a template, but you have a few things that you could fix anyway.

  1. You don't need to save the element to a variable g, since you don't use that variable for anything.
  2. You need to fix the background-color property, since color1 will not be a valid background-color value.
  3. The jQuery method can take a second parameter which is the context to search within, so thediv.find(".spanColor") could be shortened to $(".spanColor", thediv), which might not be much shorter, but a bit more readable in my opinion.
  4. You can replace .css('display','block') with the .show() method.

All-in-all, something like this perhaps:

for (var i = 0; i < arrData.length; i++)
{
    var thediv = $(".divDetails>.divRowData:first").clone().show();

    $(".spanColor", thediv).css('background-color', '#fc0');
    $(".spanText", thediv).text(arrData[i].text);

    $(".divDetails").append(thediv);
}

3 Comments

color-i is just a sample of placing data. you right about the [g] stuff anyway
what about this thediv.find(".spanColor").css('background-color', 'color'+i).andSelf().find(".spanText").text(arrData[i].text);
@RoyiNamir If you swap .andSelf() to .parent() that would work, but I believe it gets a bit harder to read.

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.