Jquery select attribute! = Includes items that do not have an attribute

I am trying to select elements that do not match a specific attribute identifier. Based on the following simplified example, why $ ("td [groupId! = 1]") returns elements that don't even have a groupId attribute? Is there any other way to select elements that have groupId where the value is not equal to 1?

<table border="1">
<tr>
    <td>Group 1</td>
    <td groupId="1">1 - 1</td>
    <td groupId="1">1 - 2</td>
    <td groupId="1">1 - 3</td>
    <td>2</td>
    <td groupId="2">2 - 1</td>
    <td>3</td>
    <td groupId="3">3 - 1</td>
    <td>Total</td>
</tr>
<tr>
    <td>1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td>2</td>
    <td groupId="2">2</td>
    <td>3</td>
    <td groupId="3">3</td>
    <td>0</td>
</tr>
<tr>
    <td>1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td groupId="1">1</td>
    <td>2</td>
    <td groupId="2">2</td>
    <td>3</td>
    <td groupId="3">3</td>
    <td>0</td>
</tr>
</table>
+3
source share
3 answers

Try $("td[groupId][groupId != 1]")

This will select all td that have groupId, and then select all td in this set that do not matter 1.

PS. This is documented non-operator behavior .

+3
source

Try:

$("td[groupId][groupId!='1']")

groupId, , 1.

+4

You can do the following: first select all td with groupId! = 1, and then select those with the groupId parameter from this jQuery set:

$('td[groupId!= 1]').filter('[groupId]');

IMHO this is completely logical, td, which does not have groupId, has groupId, which is not equal to 1, so they are added to the result set. Or put it in code:

var x = {};
typeof x.foo === 'undefined'; //true
x.foo !== 1; //true
+3
source

Source: https://habr.com/ru/post/1710500/


All Articles