-1

I have a element like this

<div class="th-class2 th-hhjjsd th-context-78474378437834873"></div>

(Note: I know class names should not be pure numbers)

I want to get the numerical number from this div.

id = 78474378437834873

Is there a way I can use regular expressions to do it. I am nearly there but it only returns the first 4 numbers.

I use a clickevent to target the div and try and get the class like this

var classString = $(this).prop("class").match(/([0-9]+)/)[1];;
console.log(classString)

result is 7847

I am just not understanding how to get the rest of the number.

Thanks

Redwall
  • 1,010
  • 5
  • 30
  • 55
  • 1
    You should not start a class with an integer – George Nov 26 '13 at 08:47
  • http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-selectors – Rafael Herscovici Nov 26 '13 at 08:48
  • Is it always 3rd position? – Samuel Nov 26 '13 at 08:48
  • I know about the integer thanks, no not always 3rd pos in classes – Redwall Nov 26 '13 at 08:49
  • @samy - finding a solution for something that is wrong at its base, is not a solution, but a hack. which might create further problems. – Rafael Herscovici Nov 26 '13 at 08:49
  • If you're using a click event and you want to get the class from it you can simply do something like this $('.element').click(function() { console.log($(this).attr('class'))}); – Mitchell Layzell Nov 26 '13 at 08:51
  • thanks but there are many classes and I only want the value of the integer not the string of classes – Redwall Nov 26 '13 at 08:52
  • 3
    Have you thought about storing the integer in a data object? Something like this,
    and then when you do your click event console.log the data-object value so something like this, $('.element').click(funtion() { console.log($(this).data('object').value)})
    – Mitchell Layzell Nov 26 '13 at 08:54
  • I am after adding a prefix now, does that help get the answer? – Redwall Nov 26 '13 at 09:00
  • -1 for learning that number only class names are wrong by the answers, and saying "I Know". if i could, i would -1 you once more, for embeding @Arun P Johny's answer into your question and not commenting on he's answer, asking for further help. – Rafael Herscovici Nov 26 '13 at 09:01
  • @Dementic - Go back to playing world of war craft please! I did not embed any answer into my question and I have been commenting on his answer which is now deleted anyway.. – Redwall Nov 26 '13 at 09:03

2 Answers2

2

You shouldn't use integers for class names because using a class typically means you are going to use the element more the once and adding a dynamic number defeats the purpose of classes, also working with someone else code and they use integers it's very hard to understand their code. As far as your questions goes, you shouldn't really use regular expressions to get a value of a class you should either store the value as an id so your element would look like this,

HTML

<div id="78474378437834873" class="th-class2 th-hhjjsd"></div>

or you could use a data object which is how I would do it like so,

HTML

<div class="th-class2 th-hhjjsd" data-object='{"value":78474378437834873}'></div>

and then when you select your element with your click event to get the value of the element you clicked console log the elements data object like so

jQuery

$('.th-class2').click(function() {
     console.log($(this).data('object').value);
});
Mitchell Layzell
  • 3,058
  • 3
  • 20
  • 33
-1

You should not use number only class names, they must start with an Alpha character [a-Z]

You can find what are the allowed characters in this discussion: Which characters are valid in CSS class names/selectors? (Please make sure to read also the comments).

As per a solution for you, The easy solution would be to use data attributes as so:

<div data-id="1000"></div>

and then you could get your id as simple as:

$(this).on('click', function() { console.log($(this).data('id')); } );

Happy Coding.

Community
  • 1
  • 1
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93