Reputation: 7504
I want to replace the HREF
s in a list with new URLs.
I have something like this:
<ul id="sidebarItems">
<li><a href="/PartBasicInfo/XX">Part Basic Info</a></li>
<li><a href="/SupplierContracts/XX">Supplier Contracts</a></li>
</ul>
I'm trying this (I know there are lots of ways to do it), but not having any luck. Any ideas?
function SetUrlParams() {
$("#sidebarItems > li > a").each(function (idx, a) {
a.attr("href", "MyURLOfChoice");
});
Upvotes: 1
Views: 226
Reputation: 10523
$('#sidebaritems > li > a').each( function(){
this.href = newURL;
});
Upvotes: 3
Reputation: 625
i tested this and it is working here
$("#sidebarItems > li > a").each(function (idx, a) {
$(a).attr("href", "MyURLOfChoice");
});
it appears that a parameter is a HtmlElement, not wrapped by jquery
Upvotes: 2
Reputation: 150263
The second parameter is the DOM element, not the jQuery element, wrap a
with $(a)
function SetUrlParams() {
$("#sidebarItems > li > a").each(function(idx, a) {
$(a).attr("href", "MyURLOfChoice");
});
}
Or leave jQuery for this simple task:
function SetUrlParams() {
$("#sidebarItems > li > a").each(function(idx, a) {
a.href = "MyURLOfChoice";
});
}
Note that you can access the DOM element with this
instead of a
.
Upvotes: 5
Reputation: 23142
The second parameter in the function passed to each
will be the DOM element, not a jQuery object. You need to use $(a)
, or just $(this)
would work as well.
Upvotes: 2
Reputation: 69915
Try this where this
refers to each of the anchor element in the matched set.
function SetUrlParams() {
$("#sidebarItems > li > a").each(function(){
this.href = "MyURLOfChoice";
});
}
Note that in your code a
refers to the dom element so you should convert it to jQuery object before calling any jQuery method if you want to keep your code.
Upvotes: 5