Passing objects into javascript functions

E

Evan

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
 
L

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.
 
R

Randy Webb

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

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?
 
F

Fred Oz

Lee said:
Evan said:

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.
 
R

Raul

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
 
M

Michael Winter

[snip]
Try this:

Please don't.

SCRIPT elements have a required type attribute.

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
 
M

Michael Winter

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.


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 ( said:
<div id="test2"></div>
<!-- End -->

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top