I have the following script which works:
classes = $(this).attr("class").split('_');
If $(this)‘s class was:
class="billed_department_employee_client_code"
The end result would look like this:
["billed", "department", "employee", "client", "code"]
This is exactly what I want as it allows me to do stuff like this
console.log( classes[0] );
console.log( classes[1] );
console.log( classes[2] );
console.log( classes[3] );
Is it possible to remove a single value from classes and move the rest of the values back?
For example,
if classes contains department, remove it. So, if it looks like this:
["billed", "department", "employee", "client", "code"]
it should become:
["billed", "employee", "client", "code"]
How can this be done?
You can use the
splice()method.will remove 1 item (second parameter) starting from index 1 (first parameter). If you don’t know the index, you can use
indexOf()to find it.Some browsers do not support
indexOf()so you can use the script from MDN to use it. You’ll also need to check to make sureindexOf()doesn’t return-1(not found) as passing in negative parameter for index intosplice()will cause it to remove items from the end of the array.