Reputation: 97
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>');
Upvotes: 1
Views: 82
Reputation: 648
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>
Upvotes: 2
Reputation: 355
look up .append() Which appends craps within any html tags with an closing tag
$('a').append("123");
Upvotes: 1