1

Is it posible to get the class name in this element if its a number? Can you check if this element has a number as a class and get it out ?

<button id="12" class="btn btn-default 1000 tBtn">
jjj
  • 1,136
  • 3
  • 18
  • 28
john
  • 796
  • 1
  • 13
  • 34

1 Answers1

2

You could use the attribute selector, [class], to select all elements with class attributes. From there you can filter the selected elements based on whether the the array of classes contain a number as a value by using the $.isNumeric jQuery method:

var $elements = $('[class]').filter(function () {
  var classes = this.className.split(' ');
  var classesWithNumbers = classes.filter(function (value) {
    return $.isNumeric(value);
  });

  return classesWithNumbers.length;
});

Here is a shorter, less verbose version:

var $elements = $('[class]').filter(function () {
  return this.className.split(' ').filter($.isNumeric).length;
});

It's worth mentioning that as of HTML4 classes and ids cannot start with numbers. However, that is no longer the case with HTML5. For reference, see: What are valid values for the id attribute in HTML?

Community
  • 1
  • 1
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304