Eric
Eric

Reputation: 21

Remove text after a certain character with Javascript or JQuery

I am using an e-commerce platform that renders the shopping cart as below inside a single div, and I would like to remove everything after the number of items in the cart, starting with the "," through to the end of "Cart".

The number of items and the Total could be any value, so I would need something that wouldn't rely upon a set variable. This would also have to work dynamically as items could be added to the cart after the page loads and would need to update accordingly.

As I am rather new to Javascript and JQuery, I would very much appreciate any help that includes the full script.

1 item(s), Total: $25.19 View Cart

Upvotes: 2

Views: 8700

Answers (2)

nnnnnn
nnnnnn

Reputation: 150030

With a regex replace - /,.*/ will find a comma followed by any number of other characters:

var text = "1 item(s), Total: $25.19 View Cart";

text = text.replace(/,.*/, "");

"This would also have to work dynamically as items could be added to the cart after the page loads and would need to update accordingly."

Well then you'd need to call the above code from wherever new items are added.

Upvotes: 2

Bruno
Bruno

Reputation: 5822

Try

var text = "1 item(s), Total: $25.19 View Cart";
text = text.split(",")[0];

Upvotes: 6

Related Questions