Results 1 to 8 of 8

Thread: getEl gives me the wrong element

  1. #1

    Default getEl gives me the wrong element

    First of all, I love yui-ext. It's a great tool and Jack Slocum is a great man for having created it. This forum is also very good and helpful.

    Now then. I have run into a small is-it-supposed-to-do-that thing here.

    I have 2 buttons with the same id, but I manage to find the DOM reference to the right one of them. However, when I do getEl and pass the reference to that function, I get an Element representing the other button. Is it supposed to do that?

    Before you say "Why do you have the same id? Are you stupid or something?" (which I have a feeling you'll say anyway :) I must point out that I don't have control over the id generation and that the buttons are located in different places in my app. Of course, that doesn't mean that I'm not stupid. :)

    It seems to me that getEl will use the id to find the element even if given a Button.

    I have found a work-around that seems to be working, but it's weird and ugly, so it would be interesting to hear if someone has any insights into this issue.

    Reproducing code:
    Code:
    <html>
    	<head>
    		<script type="text/javascript" src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/utilities_2.1.0.js"></script>
    		<script type="text/javascript" src="http://www.yui-ext.com/deploy/yui-ext/yui-ext.js"></script>
    	</head>
    	<body>
    		<script type="text/javascript">
    			function whereIsB2() {
    				var b2 = document.getElementsByTagName('button')[1];
    				var e2 = getEl(b2,true);
    				alert('Hmm, b2 is at (' + parseInt(b2.style.left) + ', ' + parseInt(b2.style.top)
    					 + ') but e2 is at (' + e2.getBox().x + ', ' + e2.getBox().y + ')');
    			}
    		</script>
    		<button id="b2" style="position:absolute; top:100px; left:100px; width:100px; height:100px;" onclick="whereIsB2();">Button</button>
    		<button id="b2" style="position:absolute; top:200px; left:200px; width:200px; height:200px;" onclick="whereIsB2();">Other button</button>
    	</body>
    </html>
    If I'm right, upon pressing a button, you will be told something along the lines of that b2 is at (200, 200) but e2 is at (100, 100).

    Regards,
    Bjrn

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default Re: getEl gives me the wrong element

    Quote Originally Posted by d3bjorn
    I have 2 buttons with the same id
    That's your problem!

  3. #3

    Default

    dude, I don't get it -- how can you expect to call a button when there is another button with the same name?

    how about calling one button1 and the other button2?

    you say your app won't allow you to do that -- well then you have to change your app.

  4. #4

  5. #5

    Default

    I told you you'd say that. :)

    So... The answer is yes, it is supposed to do that.

    Just an explanation for you guys, who are obviously upset (probably with good cause). I read the description of the el parameter ("The id of the element or the element to wrap (must have an id). If you pass in an element, it is returned") to say that it'd wrap the button I gave it. I understand now that the emphasis is on "must have an id". Said id, of course, being unique.

    Thanks for your kind help.

  6. #6
    Sencha User
    Join Date
    Apr 2012
    Location
    Austin, Texas
    Posts
    4

    Default

    One suggestion... If you don't have control over the id generation, then you may want to try exerting greater control over your selection mechanism. In other words, rather than trying to get by id, maybe you can get by class name or tag name, find the actual button you need based on something else (name?), then do your action. You could even process the form one time on load and store off each element in a MixedCollection so that you can reference them uniquely from then on without going back to the dom. Just some thoughts.

  7. #7
    Sencha User
    Join Date
    Apr 2012
    Location
    Austin, Texas
    Posts
    4

    Default

    One more thing, whoever is actually generating the ids should really change their code to make them unique -- as mentioned above, id is supposed to be unique! I'd be surprised if this didn't cause other issues with people trying to code against this dom in the future as well.

  8. #8
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Nottingham
    Posts
    30,892

    Default

    Of course if you're using the HTML onclick attribute (As opposed to Event.addListener), the resulting anonymous function runs in the scope of the target, so you could just have

    Code:
    onclick="whereIsB2(this)"
    and
    Code:
    function whereIsB2(b2) { ...

Similar Threads

  1. what does .getEl() ???
    By humpdi in forum Ext 1.x: Help & Discussion
    Replies: 2
    Last Post: 19 Mar 2007, 12:42 PM
  2. getEl Help
    By JasonMichael in forum Ext 1.x: Help & Discussion
    Replies: 2
    Last Post: 21 Feb 2007, 12:46 PM
  3. Modifying the DOM using getEl()
    By BernardChhun in forum Ext 1.x: Help & Discussion
    Replies: 9
    Last Post: 1 Feb 2007, 2:24 PM
  4. getEl() weirdness...
    By rob30UK in forum Ext 1.x: Help & Discussion
    Replies: 5
    Last Post: 17 Jan 2007, 1:45 AM
  5. YAHOO.util.Element.get('some_id') vs getEl('some_id')
    By christocracy in forum Ext 1.x: Help & Discussion
    Replies: 7
    Last Post: 24 Oct 2006, 5:05 PM

Posting Permissions

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