Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    Colfax, California USA
    Posts
    64
    Thanks
    3
    Thanked 5 Times in 5 Posts

    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?

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    are you just looking for a way to register an event handler?

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

  • #3
    New Coder
    Join Date
    Aug 2008
    Location
    Colfax, California USA
    Posts
    64
    Thanks
    3
    Thanked 5 Times in 5 Posts
    no I'm trying to pass a string into a event handler and the string to run as a function

  • #4
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    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?

  • #5
    New Coder
    Join Date
    Aug 2008
    Location
    Colfax, California USA
    Posts
    64
    Thanks
    3
    Thanked 5 Times in 5 Posts
    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.

  • #6
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    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..

  • #7
    New Coder
    Join Date
    Aug 2008
    Location
    Colfax, California USA
    Posts
    64
    Thanks
    3
    Thanked 5 Times in 5 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    My new Iphone Games Site was launched.

    lol. Check my new iphone games site for iphone and ipod touch when you have time!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •