0

I'm trying to build a JQuery selector that finds a single element with a [data-label] attribute that has any value other than blank. An example of what I've got at the moment is:

$(formSelector).find("[data-label*='-']")

Which gets me:

<fieldset data-label="vehicle-enquiry"></fieldset> 

AND

<span class="btn--icon btn__icon__pos-- dws-icon- " data-label="">Submit enquiry</span>

Which is almost exactly what I want, in that data-labels should have a '-' in their value, but I'm concerned it's not robust enough - the moment someone adds a label without a '-' it'll break.

It has to be very generic to work across multiple forms with different data-labels. It seems I can do any other kind of attribute selection in an obvious way, but not this.

LadyBernkastel
  • 447
  • 4
  • 13
  • Why would your code "break" if a label was added without `-`? It just wouldn't be found by your selector which is exactly what you want. No? I don't think you have explained what you are trying to do very clearly. – Turnip Jun 07 '16 at 14:53
  • 1
    Is there a reason why you're selecting using data attributes (which should be used for storing data and not selecting elements) ? – devlin carnate Jun 07 '16 at 14:53
  • The code is for event tracking, it's part of a JQuery selector that will find the closest data-label when an event is triggered on a form. The data-label will be the event-label for that form. – LadyBernkastel Jun 07 '16 at 14:59
  • Classes and Id's are meant for selecting elements. You should consider using a class or id, and leave data attributes for their intended purpose. – devlin carnate Jun 07 '16 at 15:13
  • I disagree - finding matching elements based on data tags is extremely useful. A simple example might be a tab-header and corresponding tab-content where both would have the same data-tab-id='x'. – freedomn-m Jun 07 '16 at 15:21

0 Answers0