2

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

4
  • 1
    var con = $("#cartelementid"); con.text(con.text().split(',')[0]); Commented Nov 26, 2012 at 21:10
  • 1
    there should be a return value containing number of items, you shouldnt have to make the split. Investigate the api for what you're using. Commented Nov 26, 2012 at 21:11
  • To avoid splitting you could always do THIS Commented Nov 26, 2012 at 21:18
  • Possible duplicate, stackoverflow.com/q/5631384/425313 Commented Apr 24, 2013 at 15:46

2 Answers 2

6

Try

var text = "1 item(s), Total: $25.19 View Cart";
text = text.split(",")[0];
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.