-1
\$\begingroup\$

I append the DOM elements dynamically, and when the data gets changed i use to hide and show lots of DOM elements. How do I improve my code?

str_user_details += '<tr ><td class="td_edu td_pad" colspan="2"><div class="div_edu"><span class="headings">Education </span> <span class="user_details_desc" id="alma_span'+key+'">'+alma_mater+'</span></div><div><span class="headings">Speciality </span> <span class="user_details_desc" id="speciality_span'+key+'">'+str_speciality+'</span></div>';         

$jq('.container').css('overflow-y','auto');
$jq('#create_account').css('display','none');
$jq('#wrong_user').css('display','none');               

if(details_length ==1) {
    $jq('.details_right_arrow').css('display','none');
} else {
    $jq('.details_right_arrow').css('display','block');
}

Its taking lots of 'if' and 'else' loops. how should i manage my code.

\$\endgroup\$
  • 6
    \$\begingroup\$ There's not enough info here to really answer the question. A decently sized sample of the actual code would probably help. \$\endgroup\$ – cHao Sep 9 '13 at 9:19
1
\$\begingroup\$

Well for one thing I know you can replace this...

$jq('#create_account').css('display','none');
$jq('#wrong_user').css('display','none');  

With...

$jq('#create_account').hide();
$jq('#wrong_user').hide()

Same with...

if(details_length ==1) {
    $jq('.details_right_arrow').hide();
} else {
    $jq('.details_right_arrow').show()
}

As for the long str_user_details... there are _underscore templates you can use...

If you have more chains of if/else statements where you are doing similar operations maybe you can pull them out and have one method that applies to them all? I would need to see more to comment further.

\$\endgroup\$
  • 2
    \$\begingroup\$ The last one can probably be done with $jq('.details_right_arrow').toggle(details_length != 1); \$\endgroup\$ – cHao Sep 9 '13 at 9:17
  • \$\begingroup\$ Thanks for your comments. I will attach further code. Also i'm loading a html page inside a div and animating that div, since the html page is heavy the animation looks choppy. Is there way to make it smoother ? \$\endgroup\$ – user1806569 Oct 9 '13 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.