1

The question to select a custom tag is asked many times.

<div mytag="foo"></div> would be $('[mytag]') or $('div[mytag]').

Now my question is about the fact that the above isn't valid HTML5. This however would be valid: <div data-mytag="foo"></div>.

Now my assumption was that -if I offer the user to use mytag="foo" or data-mytag="foo"- I could do this: $('[$mytag]'). Where $ = everything that is prefixed mytag.

Why is this wrong and how could I resolve this?

PS:
Currently I'm doing: $('[mytag], [data-mytag]') which looks design-flawish...

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • possible duplicate of [jQuery - How to select value by attribute name starts with](http://stackoverflow.com/questions/26657398/jquery-how-to-select-value-by-attribute-name-starts-with) – GreyRoofPigeon Sep 18 '15 at 10:34

1 Answers1

0

It's wrong because you can't match attribute names dynamically using an attribute selector, only attribute values. You say "$ = everything that is prefixed mytag", but there is no such syntax.

[mytag], [data-mytag] isn't exactly a design flaw by itself so much as it highlights a design flaw in the original markup. If it bothers you, you can ensure that any custom attribute the user attempts to enter is prefixed with "data-" automatically if the prefix isn't already supplied, so you never have to worry about user-defined attributes lacking the prefix.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • O, thanks for this. I wasn't aware of this. You are basically saying that calling `data-mytag` will *always* return the value of `mytag`? ...let's give it a try... :-) – Bob van Luijt Sep 18 '15 at 11:08
  • @bvl: No, I'm saying to never allow `mytag` in the first place. If the user tries to add an attribute called `mytag`, you reflect it as `data-mytag` in the markup. – BoltClock Sep 18 '15 at 11:08