473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 29002
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 then it gets passed as a copy. If you pass a...
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 assistance. specifically, I have a class called...
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 of what I need it for is as follows: I have a...
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 overall Request class. the Request Class is made from 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 corrections? ...
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 selected, it would create a variable and I would call...
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 declared static instead classes 4. objects declared...
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 pdf numbered file name. I can generate 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 which gives the error "objects of type 'Script1' do...
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 is called via some clientside javascript. I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.