Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2004, 01:04 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default changing the onclick property dynamically?

hi all,

I want to change the method name, I'm calling in an element on my page.
and I really need to pass a value through that method. It does work,but not so normally!?(pretty strange!)
assume I have a
<input type="button" id="x" onclick="">
now somewhere in the code, assuming we have a method 'y' which accepts an object as input,I try to change the onclick this way:

Neither works:

x.onclick=y(str);
nor
x.onclick=y("'"+str+"'");


:(

 
Old September 19th, 2004, 04:55 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Does str contain the value to be passed to the mathod, or it it the parameter name?
Code:
x.onclick = function{y(str);};
will evaluate str at run time and call y.
Code:
x.onclick = new Function(str, "y(" + str + ");");
will evaluate str now to be called later.

There are a number of other options if neither of these is what you meant.

--

Joe

--

Joe (Co-author Beginning XML, 3rd edition)
 
Old September 19th, 2004, 06:02 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I need the second choice(evaluating first)
But using your code,the result would be a compilation error:Expected Identifier.

I must also say that I'm using the statement in an eval method as follows:

str="";
alert("x"+i+".onclick=new Function(str,\"back(\"+str+\");\")");
eval("x"+i+".onclick=new Function(str,\"back(\"+str+\");\")");

the message in alert box is just like the statement you sent for evaluating the str first!?

alert--> x1.onclick=new function(str,"back("+str+");")
Note:you know in eval I shouldn't put a ';' at the end but I tried with ';' at the ending too and it didn't work.

I just tried it out of eval and faced the same error.I tried the 'f' in Function in both cases(f and F) and just another error.

help please! I need it badly!

 
Old September 19th, 2004, 07:50 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You'll have to show more incontext. Works fine for me. I would never use eval in these circumstances, it not necessary and is a huge performance killer as well as a memory hog.

--

Joe
 
Old September 19th, 2004, 08:43 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a complete sample code with the error I mentioned just to show you my problem.
Please just copy paste the entire thing and tell me what's wrong.
I'm in a bit hurry

<HTML>
<HEAD>
<meta name=vs_targetSchema content="HTML 4.0">
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">
<script language="javascript">
<!--
function a(){
str="2";
c2.innerText="but now it should alert something";
alert("so far so good");
c2.onclick=new Function(str, "b(" + str + ");");
}
function b(obj){
if(obj==2)alert("the onclick changed successfully!");
}
-->
</script>
</HEAD>
<BODY>
<table border="2" bordercolor="#cc3300">
<tr>
<td id="c1" onclick="a();">Sets the onclick of the next cell</td>
<td id="c2" onclick="b('1');">First calls nothing</td>
</tr>
</table>
</BODY>
</HTML>


 
Old September 19th, 2004, 11:53 PM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Really? Ain't there anybody who can answer this one!?

 
Old September 20th, 2004, 03:15 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey nerssi,

It should work if you change
Code:
c2.onclick=new Function(str, "b(" + str + ");");
to
Code:
c2.onclick=new Function("b(" + str + ");");
HTH,

Chris

 
Old September 20th, 2004, 10:25 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did my job completely elseway without changing the onclick property.
testing to see if it works is just a matter of copy and paste.
I'll try it later but I doubt it'll work...

 
Old September 21st, 2004, 12:59 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 103
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've just tried it out and it was successful!
such a pity I didn't learned it soon enough to use it when I really did need it.
thanks anyway :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
changing the backcolor property of the textbox Fehrer Access VBA 9 April 19th, 2016 10:59 AM
dynamically changing css sarah lee ASP.NET 2.0 Basics 0 January 25th, 2007 10:06 AM
dynamically changing CSS sarah lee CSS Cascading Style Sheets 1 January 25th, 2007 06:14 AM
changing property doesn't work Vanni VB.NET 4 July 1st, 2005 07:50 AM
Setting Checkbox Checked Property from OnClick saturdave ASP.NET 1.0 and 1.1 Basics 2 April 8th, 2004 09:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.