This project is archived and is in readonly mode.

#1251 ✓invalid
dukeofgaming

Element.has() and a cross-browsable "hasAttribute"

Reported by dukeofgaming | July 2nd, 2011 @ 08:57 AM | in 2.0 (closed)

Hi,

The other day I was stuck in one of those moments where doing something simple is strangely complicated (thanks IE <3). I ran into a situation where I must validate if an element property has been set, and I thought "oh, I'll just use my_element.has('someattribute')" and to my surprise there was no such thing.

Since hasAttribute is not available for older versions of IE, I started browsing through the MooTools code and I found something that looks like a possible implementation: https://github.com/mootools/mootools-core/blob/master/Source/Slick/...

Testing for it in IE7 (IE9 in IE7 mode) and IE6 (with Chrome IE Tab extension) it seems to work without problem.

I think the Element "class" could use a has method since the get method could be considered ambiguous for this purpose as it returns
null for when the attribute does not exist and for when it is empty (which IMHO should return an empty string).

Regards,

David

Comments and changes to this ticket

  • fakedarren

    fakedarren July 16th, 2011 @ 03:14 PM

    • State changed from “new” to “invalid”

    Hi David

    You can just use el.get('property') instead.

    Cheers
    Darren

  • dukeofgaming

    dukeofgaming July 16th, 2011 @ 03:40 PM

    Hi,

    While this might work for some elements with some already defined attributes, its not consistent with others that might be unknown:

    http://jsfiddle.net/XFhZx/

    See: http://ejohn.org/blog/html-5-data-attributes/

    Regards,

    David

  • fakedarren

    fakedarren July 16th, 2011 @ 04:20 PM

    • State changed from “invalid” to “open”
    • Assigned user set to “fakedarren”

    Ah. I see. Will take a look.

    Thanks
    Darren

  • fakedarren

    fakedarren July 16th, 2011 @ 10:15 PM

    Are you referring to the fact that multiple returns true/false whereas the others return null unless a value is defined?

    Eg: http://jsfiddle.net/fakedarren/XFhZx/2/ - see rel attribute

    Attributes like multiple, checked etc behave differently to value attributes (like data-*, rel, id etc etc)

    Or is this behaviour in a specific browser?

  • dukeofgaming

    dukeofgaming July 17th, 2011 @ 04:30 AM

    Not really, I don't recall the exact situation I was in in that moment, but I do remember I was trying to validate the attribute's existence itself, and since the user could set the value to empty at some point I could not really find out if it ever existed. I'm pretty sure it wasn't something that can be deemed as a bad practice though.

    I think that if slick has a way to validate if an attribute exists Element should too, mainly because checking if an attribute exists and doing it in different browsers is hard.

    This is not to treat the attribute as a boolean attribute, but to check if the attribute has ever changed (see last test at http://jsfiddle.net/XFhZx/4/ ). So, I really think Element should:

    • Have "get" corrected for the case of an empty string
    • Get a "has" method
    • And perhaps have "get" extended with a second argument for a specific value to return if the attribute does not exist at all (defaults to null).

    Oh, and its the same behavior on all browsers AFAIK.

    Regards,

    David

  • ibolmo

    ibolmo July 21st, 2011 @ 06:38 AM

    • Tag set to enhancement

    I'd say that it's worth having. Consistent API: get, set, and has.

  • Arian

    Arian August 18th, 2011 @ 12:53 AM

    At least in Slick which is in 1.4 Slick.hasAttribute will be available.

  • Christoph Pojer

    Christoph Pojer August 18th, 2011 @ 01:01 AM

    • Assigned user changed from “fakedarren” to “Arian”

    Given that Slick has it for 1.4 I can see us either add hasAttribute to Element for 1.4 or shift this ticket to 2.0 and maybe do the mapping in 2.0 or similar (or close it if 2.0 will only have Slick for this).

    Arian: what do you say?

  • Arian

    Arian August 18th, 2011 @ 01:11 AM

    • Milestone set to 2.0
    • Milestone order changed from “942” to “0”

    API wise adding Element:hasAttribute isn't really nice, however getProperty/setProperty aren't actually correct either. Anyway getProperty should only return null when the property doesn't exist, otherwise an empty string (see https://mootools.lighthouseapp.com/projects/2706/tickets/1203) or otherwise you could use Slick.hasAttribute. So at least for 1.4 I don't really see the need to add this now, maybe for 2.0.

  • ibolmo

    ibolmo January 19th, 2012 @ 10:09 AM

    • State changed from “open” to “invalid”

    Moved to Github Issues: https://github.com/mootools/mootools-core/issues/2240

    Please use Github Issues, since Lighthouse has been deprecated.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Pages