Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing objects into javascript functions

Reply
Thread Tools

Passing objects into javascript functions

 
 
Evan
Guest
Posts: n/a
 
      10-17-2004
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
 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      10-17-2004
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.

 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      10-17-2004
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?
 
Reply With Quote
 
Fred Oz
Guest
Posts: n/a
 
      10-17-2004
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.
 
Reply With Quote
 
Raul
Guest
Posts: n/a
 
      10-17-2004
(Evan) wrote in message news:<. 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
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-17-2004
On 17 Oct 2004 00:48:50 -0700, Raul <> 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.
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      10-17-2004
On Sun, 17 Oct 2004 08:03:30 -0400, shailesh <>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Passing member functions to C functions? Steven T. Hatton C++ 7 10-07-2004 02:12 AM
Problem passing objects between functions twizansky@yahoo.com C++ 5 08-19-2004 08:36 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Help! Passing Templates functions to template functions ILLOGIC C++ 1 06-01-2004 10:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57