Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-29-2008, 08:14 PM   PM User | #1
rhinodog8
New Coder

 
Join Date: Aug 2008
Location: Colfax, California USA
Posts: 64
Thanks: 3
Thanked 5 Times in 5 Posts
rhinodog8 is an unknown quantity at this point
Prototype:Event observe based on innerHTML

Ok so I can get the effect by doing

Code:
$('navbar').childElements().each(function(a){
a.setAttribute('onclick', a.innerHTML+'HTML()')})
but I don't what to cluder the generated HTML

What I want to do is this

Code:
$('navbar').childElements().each(function(a){a.observe('click', a.innerHTML+'HTML()')})
any suggestions?
rhinodog8 is offline   Reply With Quote
Old 12-01-2008, 01:55 PM   PM User | #2
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
are you just looking for a way to register an event handler?

http://www.prototypejs.org/api/event/observe
ohgod is offline   Reply With Quote
Old 12-02-2008, 07:58 AM   PM User | #3
rhinodog8
New Coder

 
Join Date: Aug 2008
Location: Colfax, California USA
Posts: 64
Thanks: 3
Thanked 5 Times in 5 Posts
rhinodog8 is an unknown quantity at this point
no I'm trying to pass a string into a event handler and the string to run as a function
rhinodog8 is offline   Reply With Quote
Old 12-02-2008, 01:52 PM   PM User | #4
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
oh my... i see what you're trying to do now.......

http://www.prototypejs.org/api/element/observe

check that syntax, this is probably one of those instances where you need to wrap your action in a function like in the example.

so a.innerHTML+'HTML()' might need to be in a function.

all that said...... wouldn't it be easier to build one function, and just pass the link specific info as an argument?
ohgod is offline   Reply With Quote
Old 12-02-2008, 09:34 PM   PM User | #5
rhinodog8
New Coder

 
Join Date: Aug 2008
Location: Colfax, California USA
Posts: 64
Thanks: 3
Thanked 5 Times in 5 Posts
rhinodog8 is an unknown quantity at this point
Quote:
Originally Posted by ohgod View Post
oh my... i see what you're trying to do now.......

http://www.prototypejs.org/api/element/observe

check that syntax, this is probably one of those instances where you need to wrap your action in a function like in the example.

so a.innerHTML+'HTML()' might need to be in a function.
I've tried that..doesn't work I think it just complies the string.
Quote:
Originally Posted by ohgod View Post
all that said...... wouldn't it be easier to build one function, and just pass the link specific info as an argument?
I don't understand exactly.. what your saying, but it sounds better than what I'm doing. Could you give me a simple example? I really want to know.
rhinodog8 is offline   Reply With Quote
Old 12-02-2008, 11:57 PM   PM User | #6
ohgod
Regular Coder

 
ohgod's Avatar
 
Join Date: Jun 2008
Location: Ohio
Posts: 579
Thanks: 6
Thanked 69 Times in 69 Posts
ohgod is on a distinguished road
um, let's see...
Code:
$('navbar').childElements().each(function(a){
				a.observe('click', function(){
						HTML(a.innerHTML);
							})
				})
calling
Code:
function HTML(what){
var clicked = what;
alert('You clicked '+clicked+'!');
}
i think that gets it..
ohgod is offline   Reply With Quote
Old 12-06-2008, 06:17 AM   PM User | #7
rhinodog8
New Coder

 
Join Date: Aug 2008
Location: Colfax, California USA
Posts: 64
Thanks: 3
Thanked 5 Times in 5 Posts
rhinodog8 is an unknown quantity at this point
thanks ohgod for that, made my code cleaner, but still didn't solve the problem.

Actually I found out about eval() a native function method that does exactly what I was trying to do.

Heres my code if anyone was wondering:
PHP Code:
eval(a.innerHTM.strip()+'()'
also you have to use strip() (from the prototype framework) because IE adds an extra space to innerHTML when called.

Update: I found a way to do this without using eval() which is supposably slow, unsecured, and frowned upon using, and not used in object oriented languages- So it's just good practice I guess.

Heres the code:
PHP Code:
c=html.strip();
myCode c+'()';
myFucn = new Function(myCode); 
and then call
PHP Code:
myFunc() 
to run.

Note: Function is capitalized.

Whole second half taken from here.

Last edited by rhinodog8; 12-06-2008 at 07:43 AM..
rhinodog8 is offline   Reply With Quote
Old 12-06-2008, 11:05 PM   PM User | #8
iphonegames
New to the CF scene

 
Join Date: Dec 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
iphonegames is an unknown quantity at this point
My new Iphone Games Site was launched.

lol. Check my new iphone games site for iphone and ipod touch when you have time!
iphonegames is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:56 PM.


Advertisement
Log in to turn off these ads.