1

Can anyone please help me why I couldn't get the right result for this code?

    Javascript:
    var items2 = $("#quadrant1");
    var coords = items2.getAttribute('coords').split(',');

    HTML:
    <map id="square_map" name="square">
        <area id="quadrant1" shape="poly" coords="206,10, 300,10, 388,10"></area>
    </map>

I've checked the typeof of items2 and it's just OBJECT, and doesn't appear to get the right HTML AREA OBJECT. All I'm after is to get the element object so I can do further manipulations like in the example to get its coords attribute value.

How do I know that it should be [object HTMLAreaObject]? I'm not sure really, but that's the typeof "this" in

$('#square_map").bind("click",function(){
var coords = this.getAttribute('coords').split(',');
... });

and it's doing its job properly with that said object. Any help would be appreciated...a lot! Thanks in advance

2
  • Does this have anything to do with your missing a quote after the list of coords? Commented Oct 17, 2012 at 10:09
  • Ah, no. Just a typo when I copy pasted. The problem's with the DOM/Jquery object after all. If I could vote up the answers below, I would. Commented Oct 17, 2012 at 11:46

2 Answers 2

2

Because items2 is not DOM Element but jQuery object in your example. Right variant:

var items2 = $("#quadrant1");
var coords = items2.attr('coords').split(',');

or

var items2 = $("#quadrant1");
var coords = items2[0].getAttribute('coords').split(',');
Sign up to request clarification or add additional context in comments.

Comments

1

If you need to get the original DOM Object use $('#quadrant1').get(). To implement its functionality jQuery uses it's own Objects, but always you can get a reference to the original DOM Object, as I mentioned above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.