-2

As I understand, $(input).not('[attribue*="string"]').val() is a collection of input with value that have attribute that has string. And if one is false, all is false. How can I isolate the one that is false? $(input).('[attribue*="string"]').val() only returns or checks the value of the first item in the collection. How can I check using ``$(input).('[attribue*="string"]')` if one of them has a value?

Here is a jsFiddle of sample code.

  • 2
    Typo of `attribute` aside, this would select all elements that look like this: `` *where the `attribute` attribute either does not exist **or** the `attribute` attribute's value does not contain the string `"string"`*. By using `val()` on a collection, it refers only to the value of the **first** element in said collection. I'm not exactly sure how to decipher the rest of your question - could you clarify? – Tyler Roper Feb 20 '19 at 18:02
  • 1
    Consider providing an example, including HTML, specifically noting which elements you would like to select, and which elements you do not. – Tyler Roper Feb 20 '19 at 18:08
  • See [Extending jQuery’s selector capabilities](https://j11y.io/javascript/extending-jquerys-selector-capabilities/); [Is it possible to select element by attribute value only?](https://stackoverflow.com/q/38308916/) – guest271314 Feb 20 '19 at 19:05
  • @TylerRoper I see. Sorry I misinterpreted the selector as I was thinking that .val() will be evaluating all the val instead of just the val of the first item in the collection. – Ferdy Reyes Feb 20 '19 at 20:11
  • @TylerRoper I've edited my question to better illustrate what I was going for. – Ferdy Reyes Feb 20 '19 at 20:32
  • I don't believe that `$(input).('[attribue*="string"]')` will do anything other than generate a syntax error. – David Thomas Feb 20 '19 at 20:34

1 Answers1

0

If the requirement is to match the element that does not have a value you can pass an empty string as value at attribute selector.

You can alternatively use jQuery.fn.filter() and .trim() the data-* property value, which evaluates to "falsy", or use RegExp. jQuery library also provides a means to create custom functions to evaluate selectors.

console.log(document.querySelectorAll("[data-text='']")[0]);

console.log($("[data-text]").filter(function() {
  return !this.dataset.text.trim()
})[0])
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-text="0">0</div>
<div data-text="">1</div>
<div data-text="2">2</div>
guest271314
  • 1
  • 15
  • 104
  • 177