...">

JQuery selector all identifiers that are integers

How to select all identifiers that are integers.

eg

<div id="1"></div>
<div id="2"></div>
<div id="3"></div>

somdething [id^="integer"]?

I know how to choose identifiers for which a similar name begins:

[id^="similar_"]
+4
source share
4 answers

Screenshot

$('[id]').filter(function () {
    return !isNaN((this.id)[0]) && this.id.indexOf('.') === -1;
}).css('color', 'red');

has an attribute selector [name]

. filter ()

isNaN ()

+4
source

You can use filter(). The equivalent [id^=integer]would be:

$('div').filter(function(){
    return this.id.match(/^\d/);
})

Only integer:

$('div').filter(function(){
    return this.id.match(/^\d+$/);
})
+7
source
$('div').filter(function(){ return this.id.match(/^\d+$/) })
+4
source

No need to use regex here ...

$('div').filter(function(){ 
    return this.id && !isNaN(this.id) && this.id % 1===0;
});

isNaN(123)     // false
isNaN('123')   // false
isNaN('foo')   // true
isNaN('10px')  // true
+3
source

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


All Articles