Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove prefix in jquery id

How to remove prefix in jquery .?

<td><span id="stu1" class="reject-student ">Not Selected</span></td>
<td><span id="stu2" class="select-student ">Selected</span></td>
<td><span id="stu5" class="select-student ">Selected</span></td>

jquery:

var selected = $(".select-student").map(function() {
return this.id; 
}).get();

i have trid like this:

var selected = $(".select-student").map(function() {
var id = $('span[id^="stu"]').remove();
return this.id; 
}).get();

I am getting result like stu1 stu2 i want to send only 1 and 2.. how can i do that.?

like image 931
Abid Avatar asked Nov 22 '25 08:11

Abid


1 Answers

You don't need $('span[id^="stu"]').remove(); the statement with remove element.

A simple solution is to use the String.prototype.replace() method to replace stu

var selected = $(".select-student").map(function() {
   return this.id.replace('stu', ''); 
}).get();

Additionally, you can also use RegEx to remove all non numeric characters

var selected = $(".select-student").map(function() {
   return this.id.replace (/[^\d]/g, ''); 
}).get();
like image 109
Satpal Avatar answered Nov 24 '25 23:11

Satpal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!