Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple fields in function

Status
Not open for further replies.

MimUK

Vendor
Oct 12, 2005
7
GB
I have some code that works great... see below.
This works for one field on screen, I also need to load the function for multiple fields

eg.
ShowOrHide ('field1', 'field2', 'Value');
ShowOrHide ('field3', 'field2', 'Value');
ShowOrHide ('field4', 'field2', 'Value');


Here is the working code

<script>
window.attachEvent ('onload', function () {

ShowOrHide ('field1', 'field2', 'Value');
// Usage: ShowOrHide (field to hide, field to monitor, hide if value selected)
});

function ShowOrHide (a, b, c)

{

var hideElem=document.getElementById("_Data"+a);

var tElem=document.getElementById("_Data"+b);

if (tElem.childNodes[0].value!=undefined)

{

tElem.childNodes[0].onchange=Function("ShowOrHide('"+a+"','"+b+"','"+c+"')");

if (tElem.childNodes[0].value==c) hideElem.parentNode.style.display="none";

else hideElem.parentNode.style.display="";

}

else if ((tElem.innerText.replace(/ +$/,'')==c)) hideElem.parentNode.style.display="none";

}

</script>

I am aware that it can be used multiple times in the same onload function, but where do I add these?

Thanks
 
Hi

I would modify the function to accept an array :
[ul]
[li]if the first parameter is an array, all itself for each item of that array[/li]
[li]otherwise work as before[/li]
[/ul]
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]ShowOrHide[/color][teal]([/teal]a[teal],[/teal] b[teal],[/teal] c[teal])[/teal]
[teal]{[/teal]
   [b]if[/b] [teal]([/teal]a [b]instanceof[/b] Array[teal])[/teal] [teal]{[/teal]
       [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]a[teal].[/teal]length[teal];[/teal] i[teal]<[/teal]l[teal];[/teal] i[teal]++)[/teal] [COLOR=darkgoldenrod]ShowOrHide[/color][teal]([/teal]a[teal][[/teal]i[teal]],[/teal] b[teal],[/teal] c[teal])[/teal]
       [b]return[/b]
   [teal]}[/teal]

   [b]var[/b] hideElem[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"_Data"[/i][/green][teal]+[/teal]a[teal]);[/teal]
   [b]var[/b] tElem[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]"_Data"[/i][/green][teal]+[/teal]b[teal]);[/teal]

   [b]if[/b] [teal]([/teal]tElem[teal].[/teal]childNodes[teal][[/teal][purple]0[/purple][teal]].[/teal]value[teal]!=[/teal]undefined[teal])[/teal] [teal]{[/teal]
       tElem[teal].[/teal]childNodes[teal][[/teal][purple]0[/purple][teal]].[/teal]onchange[teal]=[/teal][COLOR=darkgoldenrod]Function[/color][teal]([/teal][green][i]"ShowOrHide('"[/i][/green][teal]+[/teal]a[teal]+[/teal][green][i]"','"[/i][/green][teal]+[/teal]b[teal]+[/teal][green][i]"','"[/i][/green][teal]+[/teal]c[teal]+[/teal][green][i]"')"[/i][/green][teal]);[/teal]
       [b]if[/b] [teal]([/teal]tElem[teal].[/teal]childNodes[teal][[/teal][purple]0[/purple][teal]].[/teal]value[teal]==[/teal]c[teal])[/teal] hideElem[teal].[/teal]parentNode[teal].[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]"none"[/i][/green][teal];[/teal]
       [b]else[/b] hideElem[teal].[/teal]parentNode[teal].[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]""[/i][/green][teal];[/teal]
   [teal]}[/teal] [b]else[/b] [b]if[/b] [teal](([/teal]tElem[teal].[/teal]innerText[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/ +$/[/fuchsia][teal],[/teal][green][i]''[/i][/green][teal])==[/teal]c[teal]))[/teal] hideElem[teal].[/teal]parentNode[teal].[/teal]style[teal].[/teal]display[teal]=[/teal][green][i]"none"[/i][/green][teal];[/teal]
[teal]}[/teal]
Then you can call it like this :
JavaScript:
[COLOR=darkgoldenrod]ShowOrHide[/color] [teal]([[/teal][green][i]'field1'[/i][/green][teal],[/teal] [green][i]'field3'[/i][/green][teal],[/teal] [green][i]'field4'[/i][/green][teal]],[/teal] [green][i]'field2'[/i][/green][teal],[/teal] [green][i]'Value'[/i][/green][teal]);[/teal]
[small][maroon]Warning[/maroon] The above code was not tested[/small]


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top