1

I want to use jQuery to add a number to the end of a navigation menu list item. This is the number of products on another page. Currently a single list item looks something like this:

<li class="ui-accordion ui-widget ui-helper-reset" role="tablist">
<a title="New In" href="http://example.com/shop/clothing/new-in">New In</a>
</li>

However I want to add the number of items directly after 'New' for example 'New (20)'. This number is located on items href, in the following place on that page:

<div class="paging">
<span class="itemcount">174</span>
</div>

Each list item has a different href but the same class which contains the unique number that I need. Any ideas?

I have tried:

$("#ui-id-12 > .ui-accordion > li:eq(0) > a:eq(0)").replaceWith('<a title="New In" href="http://example.com/shop/clothing/new-in">New In ' + $(this).load("http://m.example.com/shop/clothing/new-in.html .itemcount") + '</a>');
2
  • 1
    are you not using a database for your items? if not, then how are you populating them? if you're using a server side script, I dont see the issue. If you're using javascript to pull from a CSV file or something, I still dont see the issue. Commented Feb 18, 2015 at 2:30
  • [OBJECT OBJECT] is being printed Commented Feb 18, 2015 at 4:38

2 Answers 2

2

Here is a simple way for you to get the value you are looking for:

$(this).load("http://m.example.com/shop/clothing/new-in.html", function(data){
  alert(data.match(/<span class="itemcount">(\d+)<\/span>/)[1]);
  $("#span-id").text(data.match(/<span class="itemcount">(\d+)<\/span>/)[1]);
  // for putting stuff into a <span> :)
});

You might want to make a <span> with a specific ID to put the loaded content into like so:

<a title="New In" href="http://example.com/shop/clothing/new-in">New In <span id="span-id"></span></a>
Sign up to request clarification or add additional context in comments.

Comments

1

look up .append() Which appends craps within any html tags with an closing tag

$('a').append("123");

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.