On May 11, 2:23 pm, waseem sabjee <waseemsab...@gmail.com> wrote: > var t = $("#myid").text(); > $ ("#myid").text(t.replace(' left in stock',''); > t = $("#myid").text(); > if(t == "1") { > $ ("#myid").text(t.replace(t,'only '+t+' item remain');} else { > > $ ("#myid").text(t.replace(t,t+' items remaining'); > > }
There are simpler and more robust ways to do that than replacing the exact phrase: Doing a regex replace using a back-reference: var $stock = $('#myel'); $stock.text( $stock.text().replace(/.*(\d+).*/, 'Only $1 remaining') ); // $1 is a back reference to the (last) digit matched in the string, in between parenthesis Or simply grabbing the number: var $stock = $('#myel'); var num = /\d+/.exec( $stock.text() )[0]; $stock.text('Only '+num+' remaining'); cheers -- ricardo