How make this jquery function reusable?

Posted by Pandiya Chendur on Stack Overflow See other posts from Stack Overflow or by Pandiya Chendur
Published on 2010-04-03T06:59:11Z Indexed on 2010/04/03 7:03 UTC
Read the original article Hit count: 192

Filed under:
|

Comsider this jquery function,

function showresults(jsonobj)
{
   $.each(jsonobj, function(i, employee) {
            divs += '<div class="resultsdiv"><br /><span style="display: inline-block;width:150px;" class="resultName">' + employee.Emp_Name + '</span><span class="resultfields" style="padding-left:100px;">Category&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.Desig_Name + '</span><br /><br /><span id="SalaryBasis" class="resultfields">Salary Basis&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.SalaryBasis + '</span><span class="resultfields" style="padding-left:25px;">Salary&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.FixedSalary + '</span><span style="font-size:110%;font-weight:bolder;padding-left:25px;">Address&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + employee.Address + '</span></div>';
            });

}

Here i am iterating my json data and drawing div elements.... As you see these div elements are hardcoded how to pass these html div elements dynamically by a variable...

var divs=<div></div>
function showresults(jsonobj,divs)
{
   $.each(jsonobj, function(i, employee) {
            divs += divs

Is this correct way of doing but there will be an error employee object wont be there know when my divs variable declared outside the function... Any suggestion...

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about JSON