Rich-text Reply

Insert text in the middle of some strings

miguelm 05-23-16

Insert text in the middle of some strings

Hi,

 

I need a function to add the text _NHR_  in the middle of the elements IDs, when the element contains the text 1_BAR, for example if I have this value:

 

'$("#form_datos_pago_96713_1_BAR_14 > .btn").click();',

 

The function should transform it to:

 '$("#form_datos_pago_96713_1_NHR_BAR_14 > .btn").click();',

Is this possible?

 

Thank you,

Level 2

Re: Insert text in the middle of some strings

Hi Miguel,

 

Thanks for posting your question here! If you're looking to change elements, what you can do is: find all elements containing that ID, and then change their ID and add _NHR_:

 

var identifier = "1_BAR";
var insert_before = "_BAR";
var to_insert = "_NHR";
$('*[id*=identifier]').each(function() {
  new_id = [this.id.slice(0,this.id.indexOf(insert_before)), to_insert, this.id.slice(this.id.indexOf(insert_before))].join('')
  this.id = new_id;
});

 

I got the ideas for how to do it from:

 

Please note that this does not fire the click again, so if you'd like to fire a click, you'll have to do that again too.

 

Is this what you're looking for?

 

Best wishes,

Nils

miguelm 05-24-16
 

Re: Insert text in the middle of some strings

Thank you Nils, I'm going to try this

Level 2