I have the following HTML:
<li class="workorder" id="workorder_7">
I would like to use the digit after the _ in some javascript. In this case, I need the integer 7
How would I do that?
Thanks!!
There are multiple ways to do that: regular expressions, substring matching, and others. One of the easiest is to just use split to break the string into an array, and grab the last array element:
var str_id = "workorder_7";
var id = str_id.split('_')[1];
You can also use .pop as suggested by VisioN to get the last element from the array. Then it would work with a string with any number of underscores, provided the numeric id is the last one:
var str_id = "main_workorder_7";
var id = str_id.split('_').pop();
.length-1 as the index..split("_").pop() instead.