Jquery: set href attribute at runtime using jquery

I have sầu a mix of liên kết on my page & some of them start with /mypath, like this

I need lớn add a querystring to the links that start this way, so the result is something lượt thích with the "1234" being the node id of the node we"re currently viewing.

Bạn đang xem: Jquery: set href attribute at runtime using jquery

I"ve sucessfully passed the nid into my js file using obatambeienwasirherbal.com settings. The only part of this I can"t get to lớn work is putting the original link in where you see the second attr("href").

My code is as follows:

$("a").attr("href", attr("href") + "?parent=" + obatambeienwasirherbal.com.settings.nodeinfo.nid);Instead of that second attr("href") I"ve sầu tried a number of things and can"t figure out what it should be.

Improve sầu this question
asked Jun 22 "12 at 16:50

46466 silver badges1818 bronze badges
Add a phản hồi |

1 Answer 1

Active Oldest Votes
You"ve got nothing khổng lồ hook onto lớn for that second attr() as you"re not storing the result of the jQuery Gọi in a variable.

Xem thêm: Nghĩa Của Từ Qrt Là Gì - Qrt Là Gì, Nghĩa Của Từ Qrt

I"d advise using a loop instead:

$("a").each(function(index, element) var newAttr = $(element).attr("href") + "?parent=" + obatambeienwasirherbal.com.settings.nodeinfo.nid; $(element).attr("href", newAttr););That will catch any liên kết on the page that matches the path selector.

Improve sầu this answer
answered Jun 22 "12 at 17:22

159k1515 gold badges270270 silver badges313313 bronze badges
Add a comment |

Your Answer

Thanks for contributing an answer to obatambeienwasirherbal.com Answers!

Please be sure to answer the question. Provide details & mô tả your research!

But avoid

Asking for help, clarification, or responding to lớn other answers.Making statements based on opinion; bachồng them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using E-Mail và Password

Post as a guest

Email Required, but never shown

Post as a guest

Thư điện tử

Required, but never shown

Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree lớn our terms of service, privacy policy & cookie policy

Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.

The Overflow Blog
Featured on Meta
How to add Jquery plugin khổng lồ obatambeienwasirherbal.com?
Use Regex khổng lồ insert jQuery querystring before a url anchor
How khổng lồ get a themed form via Ajax in D7?
How vì chưng I add jQuery di động.js?
How to add an autocomplete field lớn a khung
Images sent via AJAX, saved to database but not shown on the node
Colorbox after ajax load
Views Slideshow Cycle doesn't re-initialize properly after an AJAX nội dung update
Hot Network Questions more hot questions

Question feed
Subscribe to RSS
Question feed To subscribe to this RSS feed, copy and paste this URL into lớn your RSS reader.


obatambeienwasirherbal.com Answers
Staông chồng Exchange Network
site kiến thiết / biệu tượng công ty © 2021 Staông chồng Exchange Inc; user contributions licensed under cc by-sa. rev2021.3.18.38837