/    Sign up×
Community /Pin to ProfileBookmark

Modifying anchor href

Can I, and how would I use js to modify the href in an anchor?

<td class=’nav2t’ id=’LOC1′>
<a href=”javascript:doThis(‘LD1′)” name=’LD1’>LOC 1</a></td>

I can modify the innerHtml with
document.getElementById(LOC1).innerHTML=’new loc 1′

but nothing I try to modify the href to say javascript:doThat(‘LS1’) seems to work. Also, when I modify the innerHTML the original href doesn’t even work.

tried
document.getElementsByTagName(“a”).LD1.href=javascript:doThat(‘LS1’);
and
var anchors = document.getElementsByTagName(“a”);
anchor=anchorsÝi¨;
anchor.setAttribute(“href”,”javascript:doThat(‘LS1’)”);

any direction would be appreciated. Thanks

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@CharlesOct 04.2006 — Don't use the "javascript" pseudo URL scheme and you're best not even using a A element if it isn't going to be a real link. And use something like document.getElementById('LS1').onclick = function () {doThat ('LS1')}Though, I would use document.getElementById('LS1').onclick = doThatand then refer to the element as "this" inside of doThat.
Copy linkTweet thisAlerts:
@hack_jsauthorOct 04.2006 — Thanks for responding. Looks like a much simpler solution. However I am missing something. I changed code to;

<td class='nav2t' id='LOC1'


onclick='javascript:makeCall()'>LOC Tab1</td>

then in the script I try;

document.getElementById(LOC1).innerHTML="History";

document.getElementById(LOC1).onclick=

'javascript:makeCall'data1','data2')';

'History' replaces 'LOC Tab1' but the ability to 'onclick' disappears. I do not get an error though for the replace.

what am I missing? Thanks
Copy linkTweet thisAlerts:
@CharlesOct 04.2006 — what am I missing? Thanks[/QUOTE]You're not following either of my examples.
Copy linkTweet thisAlerts:
@hack_jsauthorOct 04.2006 — I suspect your answer would be very clear if I knew what I was doing ? . What I am trying to accomplish is to create/load a personal navigation bar based on values that I stored in a cookie. Each person could have different values in the bar. I did at least take out the <A> :o .

document.getElementById('LOC1').onclick=

'javascript:makeCall'data1','data2')';

(this one does have the onclick element.

In the orginal definition I don't really need to have any onclick value defined. I only want to set that when I build the individual navbar.

Thanks
Copy linkTweet thisAlerts:
@hack_jsauthorOct 04.2006 — Thanks Charles, as you know you had it exactly right, I didn't understand the concept of what you where trying to do with the { } so your answer flew over my head. I appreciate your help.
Copy linkTweet thisAlerts:
@hack_jsauthorOct 04.2006 — Another problem, using your method I was able to load the onclick and when my 'buttons' are created they will invoke my javascript. The problem is I am going through a loop and loading 5 buttons and all buttons try and execute the last version of the onclick I loaded.

(both locx and lc1 are being set in the loop)

document.getElementById(locx).onclick=function() {makeCall ('LOC',lc1)}
×

Success!

Help @hack_js spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...