Wednesday, December 23, 2009

[jQuery] Re: js function to jQuery

On Dec 23, 6:58 am, jqHunter <tinu.punn...@gmail.com> wrote:
> Hi All,
>
> I have the following js function used in an old web app. How can I
> translate this to equivalent jQuery code? Appreciate your help!
>
> function expandCollapse_onclick()
> {
>     var treeCell = document.getElementById("TreeviewTd");
>     var topmenuCell = document.getElementById("MenuBarTd");
>     var collapseExpandCell = document.getElementById
> ("CollpaseExpandTd");
>     if (treeCell.style.display == '')
>     {
>         treeCell.style.display = 'none';
>         topmenuCell.style.display = 'none';
>     }
>     else
>     {
>         treeCell.style.display = '';
>         topmenuCell.style.display = '';
>     }
> }

You could consider re-factoring the functions into a library so you
write less code, e.g.

function toggle() {
var i = arguments.length,
el,
s;
while (i--) {
el = getEl(arguments[i]);
s = el && el.style;
if (s) {
s.display = (s.display == 'none')? '' : 'none';
}
}
}

function getEl(id) {
return (typeof id == 'string')? document.getElementById(id) : id;
}

Now the click function becomes:

toggle('TreeviewTd','MenuBarTd');


--
Rob

No comments: