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");
});
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");
});
Share
Improve this question
edited May 7, 2012 at 16:35
gdoron
150k59 gold badges302 silver badges371 bronze badges
asked May 7, 2012 at 15:24
birdusbirdus
7,52417 gold badges64 silver badges95 bronze badges
5
- Also, why am I not seeing the formatting toolbar when I make my posts? Would like to mark code blocks. – birdus Commented May 7, 2012 at 15:25
- Re: your ment, no idea. It works just fine for me. Ask on Meta. – Matt Ball Commented May 7, 2012 at 15:27
- Re the disappearing formatting toolbar, I don't think it has anything to do with you particularly. That happens to me once in a great while. Refreshing the page can fix it. – DOK Commented May 7, 2012 at 15:32
- I've verified that the loop is executing and that "this.href" is, in fact, the old hyperlink. However, when the loop is done executing and I hover over the menu items, they still have their old values. – birdus Commented May 7, 2012 at 15:58
-
@birdus Use
$(this).attr('href', "foo")
if you want to change the HTML element, not just the variable. – gdoron Commented May 7, 2012 at 16:36
5 Answers
Reset to default 5The 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
.
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.
$('#sidebaritems > li > a').each( function(){
this.href = newURL;
});
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.
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