Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
jQuery Remove string from string

I am trying to remove a string from a string in jQuery.

Here is the string:

username1, username2 and username3 like this post.

I would like to remove username1, from this list. I tried adding the list to an array using .split(', ') but I got an error. I'm assuming the error is because not every word has a comma after it.

I always want to remove the fist item from the list. username1 is just an example username. The first item will always be the username of the currently logged in user if they have liked this post.

I have tried:

  var updated_list = $('#post_like_list').html().replace('username1, ', '');
  $('#post_like_list').html(updated_list);

But that didn't update the list. It does however update the list when using .text() instead of .html() but I have links inside the list which I need to preserve.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

pretty sure you just want the plain old replace function. use like this:

myString.replace('username1','');

i suppose if you want to remove the trailing comma do this instead:

myString.replace('username1,','');

edit:

here is your site specific code:

jQuery("#post_like_list-510").text().replace(...) 
over 4 years ago · Santiago Trujillo Report

0

To add on nathan gonzalez answer, please note you need to assign the replaced object after calling replace function since it is not a mutator function:

myString = myString.replace('username1','');
over 4 years ago · Santiago Trujillo Report

0

If you just want to remove "username1" you can use a simple replace.

name.replace("username1,", "")

or you could use split like you mentioned.

var name = "username1, username2 and username3 like this post.".split(",")[1];      
$("h1").text(name);

jsfiddle example

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!