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 6 of 6
  1. #1
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts

    Question JSON object loop

    To return, onsomeevent, the index of some elements (HTML) in their collection, I use a 'faux' attribute, ex:
    PHP Code:
    var myEl =document.getElementsByTagName('div');
    for(var 
    i=0;i<myEl.length;i++){
    myEl[i].myAtt=i;//a faux attribute
    myEl[i].onclick=function(){alert(this.myAtt)}

    So far so good. I wanted to extend this ideea upon Objects (in JSON), but it looks like the trick woun't make it:

    PHP Code:
    var funcObj={
    'onclick':document,
    'onkeypress':document,
    'onkeydown':document
    }
    for(
    ev in funcObj){
    funcObj[ev].myAtt=ev;
    funcObj[ev][ev]=function(){alert(this.myAtt)}

    Where the error in my judgment? (I know objects are unordored collection, do not tell me that)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    funcObj[ev] references the document object for each ev in funcObj. Thus, you are assigning document.myAtt = ev during each iteration in the loop, leaving the value at the last one that occurs in the loop. You then assign document.[b]eventname[/n] = function() { alert(this.myAtt) }
    So it will simply alert the last value assigned to myAtt, which explains why clicking generates an alert saying "onkeydown".

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Well I saw that, so maybe I should reformulate. Is it possible to attach a 'faux' attribute to an event? After all an event may be treatead like an object as well... Or the whole conecpt is an endless way?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    hmm
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var funcObj={
    'onclick':document,
    'onkeypress':document,
    'onkeydown':document
    }
    var Evt=0;
    for(ev in funcObj){
    funcObj[ev][ev+'T']=ev;
    funcObj[ev][ev]=function(event){  if (!event) var event=window.event;
    alert(this['on'+event.type+'T'])}
    }
    
    //-->
    </script>
    
    </head>
    
    <body>
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Personally, I'd probably skip that property and use a closure instead. Here's a couple of possible solutions to your problem though. (Untested as usual...)
    Code:
    var
        ev,
        funcObj={
            'onclick':document,
            'onkeypress':document,
            'onkeydown':document};
    
    for(ev in funcObj){
    solution0:
        funcObj[ev][ev]=function(youreventdata){
            return function(e){
                if(!e)
                    e=window.event;
                alert(youreventdata);};}(ev);
    
    solution1:
        funcObj[ev][ev]=function fn(e){
            if(!e)
                e=window.event;
            alert(fn.youreventdata);};
        funcObj[ev][ev].youreventdata=ev;
    
    solution2:
        funcObj[ev][ev]=function(e){
            if(!e)
                e=window.event;
            alert(arguments.callee.youreventdata);};
        funcObj[ev][ev].youreventdata=ev;
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Yes, thank you all. I knew that I could have used event/window.event. I was just curious if there could be another way to "catch" the name of the event on treating it same as a simple JSON object, not like a special event Object. It looks like there is no such a way.

    Thank you all.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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