Making a Javascript array of multiple CSS classes assigned to an element with jQuery
-
06-07-2019 - |
Question
MY javascript: Updated Again.
$('.calc').change(function(){
var classArray = $(this).attr('class').split(',');
$.each(classArray, function(){
alert(classArray);
});
});
And the input:
<input type="text" class="calc R#r# C#i#" />
The pound signs are variables, I'm using ColdFusion.
What I need to be able to do is successfully take each class and place them in an array. That way I can use that to do the required calculations for the entire table.
Help would be appreciated.
Thanks
Solution
You could simply split the class attribute value, using space as the separator:
$('.calc').change(function(){
var classArray = $(this).attr('class').split(' ');
});
Edit: I think that you want to do this.
$('.calc').change(function(){
var classArray = $(this).attr('class').split(' ');
$.each(classArray, function(){
alert(this);
});
});
Try this running example.
OTHER TIPS
CMS' answer definitely works for me. I doubt this is a browser issue but I suppose that's a possibility?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow