467,169 Members | 996 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

Passing objects into javascript functions

I have a fairly simple problem that I just cant seem to figure out. I
am trying to pass and use a div in a function. This is what I have so
far... it doesnt work though...

<script>
function divControl(divName){
divName.innerHTML="test"
}
</script>

in the HTML I have three div's

<div id="test1"></div>
<div id="test2"></div>
<a onclick="divControl('test1')">test3</a>

The system does not seem to recognize the divName in the function ?!?!

Any help would be greatly appreciated

Evan
Jul 23 '05 #1
  • viewed: 28643
Share:
7 Replies
Evan wrote:
I have a fairly simple problem that I just cant seem to figure out. I
am trying to pass and use a div in a function. This is what I have so
far... it doesnt work though...

<script>
function divControl(divName){
divName.innerHTML="test"
}
</script>

in the HTML I have three div's

<div id="test1"></div>
<div id="test2"></div>
<a onclick="divControl('test1')">test3</a>

The system does not seem to recognize the divName in the function ?!?!

Any help would be greatly appreciated

Evan

http://jibbering.com/faq/#FAQ4_41

Addresses that issue directly.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
Jul 23 '05 #2
Lee
Evan said:

I have a fairly simple problem that I just cant seem to figure out. I
am trying to pass and use a div in a function. This is what I have so
far... it doesnt work though...

<script>
function divControl(divName){
divName.innerHTML="test"
}
</script>

in the HTML I have three div's

<div id="test1"></div>
<div id="test2"></div>
<a onclick="divControl('test1')">test3</a>

The system does not seem to recognize the divName in the function ?!?!

Any help would be greatly appreciated

You're not passing an object. You're passing a string
containing the id of an object. That string does not
have an innerHTML attribute.

You can either pass a reference to the object, or you can
try to obtain a reference to the object in the function:

<script type="text/javascript">
function divControl(divName){
var divRef=document.getElementById(divName);
divRef.innerHTML="test"
}
</script>
In a production script, there should be error checking, too.

Jul 23 '05 #3
Lee wrote:
Evan said:
I have a fairly simple problem that I just cant seem to figure out. I
am trying to pass and use a div in a function. This is what I have so
far... it doesnt work though...


Another method is to pass the object reference, rather than just a
string. This can be simpler in some cases, the OP's script stays the
same but the HTML changes to:

...
<a onclick="divControl(document.getElementById('test1 '))">test3</a>

The difference here is that you are not bound to use getElementById, so
you can use any method (this, this.parentNode, this.form, etc.) to get
the reference and pass it to the function.

Cheers, Fred.
Jul 23 '05 #4
ev*****************@hotmail.com (Evan) wrote in message news:<53**************************@posting.google. com>...
I have a fairly simple problem that I just cant seem to figure out. I
am trying to pass and use a div in a function. This is what I have so
far... it doesnt work though...

<script>
function divControl(divName){
divName.innerHTML="test"
}
</script>

in the HTML I have three div's

<div id="test1"></div>
<div id="test2"></div>
<a onclick="divControl('test1')">test3</a>

The system does not seem to recognize the divName in the function ?!?!

Any help would be greatly appreciated

Evan


Try this:
<script>
function divControl(divName){
divName.innerHTML="test"
}
</script>
</HEAD>
<div id=test1></div>
<div id=test2></div>
<a href='' onclick="divControl(test1);return false">test3</a>

Cheers
Raul
Jul 23 '05 #5
On 17 Oct 2004 00:48:50 -0700, Raul <dd*****@arrakis.es> wrote:

[snip]
Try this:
Please don't.
<script>
SCRIPT elements have a required type attribute.

<script type="text/javascript">
function divControl(divName){
divName.innerHTML="test"
}
</script>
</HEAD>
<div id=test1></div>
<div id=test2></div>
<a href='' onclick="divControl(test1);return false">test3</a>


You still aren't passing an object reference, just an undefined global
variable. That will do nothing but cause an error. I suggest you read the
FAQ (<URL:http://jibbering.com/faq/>), specifically section 4.41.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
There is no onClick for "<a>" anchor Tag's and the innerHTML will embed
HTML not Text.Check Out New Code it Should Work.

<SCRIPT LANGUAGE="JavaScript">
<!--
//Javascript for Div Tag Creation.
function divControl(divName,divName1){
divName.innerText="test";
divName1.innerText = "Deepak Kini";
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<!--Insert Text into Div Tags-->
<div id="test1"></div>
<a href="javascript:divControl(test1,test2)">test3</a>
<div id="test2"></div>
<!-- End -->
Jul 23 '05 #7
On Sun, 17 Oct 2004 08:03:30 -0400, shailesh <sh*******@vasnet.co.in>
wrote:
There is no onClick for "<a>" anchor Tag's
Nonsense. Of course there is!
and the innerHTML will embed HTML not Text.
Wrong. If you assign only text, only text will be displayed (assuming
innerHTML is supported).
Check Out New Code it Should Work.
In IE, but few others. Better solutions have already been suggested.
<SCRIPT LANGUAGE="JavaScript">
The language attribute is deprecated in favour of (required) type.

<script type="text/javascript">
<!--
Attempting to hide scripts is also an out-dated practice. All user agents
currently in use know what a SCRIPT element is, so they will not render
its contents even if they cannot execute the script.
//Javascript for Div Tag Creation.
function divControl(divName,divName1){
divName.innerText="test";
divName1.innerText = "Deepak Kini";
The innerText property is a proprietary Microsoft mechanism that is not
supported very well by other browsers. Unless you're writing for IE only,
and the OP gave no indication of that, don't use it.
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<!--Insert Text into Div Tags-->
<div id="test1"></div>
<a href="javascript:divControl(test1,test2)">test3</a>
You need to read the group FAQ (<URL:http://jibbering.com/faq/>).
<div id="test2"></div>
<!-- End -->


Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by twizansky@yahoo.com | last post: by
reply views Thread by hawklord451 | last post: by
4 posts views Thread by sofeng | last post: by
14 posts views Thread by Jess | last post: by
1 post views Thread by billsahiker@yahoo.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.