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!

Passing to a Function

Status
Not open for further replies.

hiyatran

Technical User
Aug 7, 2010
21
CA
How do I pass an id to a function??
Or is this even possible.

Code:
<html>
<head>
<title>
  JavaScript array passing example
</title>
<script type="text/javascript">

function init(){
   display(addText1);
   display(addText2);
   display(addText3);
   display(addText4);
}

function display(var myText){
  var newText = '<h2>' + "Will this change anything..." + '</h2>';

   document.getElementById('myText').innerHTML = newText;
}
</script>
</head>
<body onload="init()">

<div id="addText1"></div>
<div id="addText2"></div>
<div id="addText3"></div>
<div id="addText4"></div>
</body>
</html>

 
Hi

The value of [tt]id[/tt] property is just a string, you can pass it just as you pass any string. But you have to use that value, not another string literal :
Code:
[b]function[/b] [COLOR=darkgoldenrod]display[/color][teal]([/teal]myText[teal])[/teal][teal]{[/teal]
  [b]var[/b] newText [teal]=[/teal] [green][i]'<h2>'[/i][/green] [teal]+[/teal] [green][i]"Will this change anything..."[/i][/green] [teal]+[/teal] [green][i]'</h2>'[/i][/green][teal];[/teal]

   document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal]myText[teal]).[/teal]innerHTML [teal]=[/teal] newText[teal];[/teal]
[teal]}[/teal]

Feherke.
 
How do I pass an id to a function??

Use functionName(this.id) in the event handler.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top