472,341 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,341 software developers and data experts.

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
7 28950
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax...
5
by: twizansky | last post by:
Hello, I have a problem with values of object properties changing seemingly on their own. I am completeley baffled and would appreciate any...
3
by: Nikki | last post by:
Hi, I would like to create a central storage place for javascript functions that I can then access from any of my asp.net projects. An example...
0
by: hawklord451 | last post by:
To get a foot hold in the area of web services I have created the following small project. I have created a group of classes to describe a...
4
by: sofeng | last post by:
The following link shows a chart I created about passing structures among functions. Would you review it and tell me if it requires any...
1
satterfieldben
by: satterfieldben | last post by:
I have a newbie question about passing variables between functions. I am wanting to use a drop down box to select a value. Then base on which was...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object...
2
polymorphic
by: polymorphic | last post by:
I am no longer good at Javascript and need help. I'm trying to embed pdf files in html then build some sort of navigation between the pdfs via the...
1
by: billsahiker | last post by:
I have some javascript functions in an xslt file. Everything works and produces the desired html, except when using the visual studio debugger...
3
by: JJ | last post by:
I am using a handler (processImage.ashx) to display an image. The image is displayed according to parameters passed in the querystring. The handerl...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.