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.


javascript
Share
Improve sầu this question
Follow
asked Jun 22 "12 at 16:50
*

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

1 Answer 1


Active Oldest Votes
1
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.


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

Clive♦Clive
159k1515 gold badges270270 silver badges313313 bronze badges
2
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
Submit

Post as a guest


Name
Email Required, but never shown


Post as a guest


Name
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
Related
19
How to add Jquery plugin khổng lồ obatambeienwasirherbal.com?
0
Use Regex khổng lồ insert jQuery querystring before a url anchor
0
How khổng lồ get a themed form via Ajax in D7?
0
How vì chưng I add jQuery di động.js?
1
How to add an autocomplete field lớn a khung
1
Images sent via AJAX, saved to database but not shown on the node
0
Colorbox after ajax load
0
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.


*

default
obatambeienwasirherbal.com Answers
Company
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