views:

291

answers:

2

I have created a parent div and inserted div's after the parent div using jquery insertAfter() method.... What happens is My first record goes to the bottom and next record gets inserted above it....

Here is my function...

function Iteratejsondata(HfJsonValue) {
    var jsonObj = eval('(' + HfJsonValue + ')');
    for (var i = 0, len = jsonObj.Table.length; i < len; ++i) {
        var employee = jsonObj.Table[i];
        $('<div  class="resultsdiv"><br /><span id="EmployeeName" style="font-size:125%;font-weight:bolder;">' + employee.Emp_Name + '</span><span style="font-size:100%;font-weight:bolder;padding-left:100px;">Category&nbsp;:</span>&nbsp;<span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis&nbsp;:</span>&nbsp;<span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary&nbsp;:</span>&nbsp;<span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address&nbsp;:</span>&nbsp;<span>' + employee.Address + '</span></div>').insertAfter('#ResultsDiv');
    }
}

And my result is,

alt text

Palani must be next to my parent div but it is at the bottom... Because insertAfter() inserts every record next to the #ResultsDiv ... Any suggestion how to insertafter the newly generated div...

EDIT: how to add row color to these divs i used

 function Iteratejsondata(HfJsonValue) {
 var jsonObj = JSON.parse(HfJsonValue);
 for (var i = jsonObj.Table.length - 1; i >= 0; i--) {
    var employee = jsonObj.Table[i];
    $('<div id="resDiv" class="resultsdiv"><br /><span id="EmployeeName" style="font-size:125%;font-weight:bolder;">' + employee.Emp_Name + '</span><span style="font-size:100%;font-weight:bolder;padding-left:100px;">Category&nbsp;:</span>&nbsp;<span>' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" style="font-size:100%;font-weight:bolder;">Salary Basis&nbsp;:</span>&nbsp;<span>' + employee.SalaryBasis + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Salary&nbsp;:</span>&nbsp;<span>' + employee.FixedSalary + '</span><span style="font-size:100%;font-weight:bolder;padding-left:25px;">Address&nbsp;:</span>&nbsp;<span>' + employee.Address + '</span></div>').insertAfter('#ResultsDiv');
}
$("#resDiv.resultsdiv:odd").css("background-color", "#F4F4F8");
$("#resDiv.resultsdiv:even").css("background-color", "#EFF1F1");
}

But didnt work..

+1  A: 

Reverse the loop:

for (var i = jsonObj.Table.length - 1; i >= 0; i--)

Remark: don't use eval. I would recommend you JSON.parse instead or if this JSON is coming from an ajax call jQuery should automatically parse it to object.


UPDATE:

To add row color to these divs you could try this outside the loop:

$(".resultsdiv:odd").css("background-color", "#F4F4F8");
$(".resultsdiv:even").css("background-color", "#EFF1F1");
Darin Dimitrov
`jsonObj.Table is undefined error`
Pandiya Chendur
@Darin if didnt eval it i get the above error
Pandiya Chendur
`var jsonObj = JSON.parse(HfJsonValue);`
Darin Dimitrov
@Darin that worked...
Pandiya Chendur
@Darin look at my edit...
Pandiya Chendur
Please see my update.
Darin Dimitrov
@Darin now see my edit
Pandiya Chendur
@Darin it worked thanks a lot i removed `#resDiv`
Pandiya Chendur
@Darin is it possible to add onhover to these divs...
Pandiya Chendur
Of course: `$('.resultsdiv').hover(function() { alert('hovering'); });`
Darin Dimitrov
@Darin that worked but how to assign a color..
Pandiya Chendur
@Darin found that too `$(this).css('background-color', '#F00');` inside the function...
Pandiya Chendur
+1  A: 

You are adding elements after the parent element:

<div id="ResultsDiv">
</div>
<div class="resultsdiv">...4...</div>
<div class="resultsdiv">...3...</div>
<div class="resultsdiv">...2...</div>
<div class="resultsdiv">...1...</div>

but you probably want to add elements inside the parent element:

<div id="ResultsDiv">
  <div class="resultsdiv">...1...</div>
  <div class="resultsdiv">...2...</div>
  <div class="resultsdiv">...3...</div>
  <div class="resultsdiv">...4...</div>
</div>

Use the appendTo method instead of the insertAfter method.

Update:

You are adding several elements with the same id, which is not legal. Just use the class to target the elements:

$(".resultsdiv:odd").css("background-color", "#F4F4F8");
$(".resultsdiv:even").css("background-color", "#EFF1F1");
Guffa
@Guffa look at this question you ll know what i am doinghttp://stackoverflow.com/questions/2498179/add-divs-below-a-parent-div-using-jquery
Pandiya Chendur
@Pandiya: Then your "parent" element is really just the first sibling. Append the elements to the parent of the first element instead.
Guffa
@guffa thats y i used `insertAfter()` method.. Is it correct or am i missing something here?
Pandiya Chendur
@guffa also look at my edit
Pandiya Chendur
@Pandiya: If you use the `insertAfter` method you can only insert elements next to the "parent" element. If you add a real parent element you can add elements at the end of the list.
Guffa