Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing href on the fly

Status
Not open for further replies.

pahiker

Programmer
Aug 31, 2012
45
0
0
US
Hi, newbie here.

I am in the process of revising a website, one of the tasks is to create a pair of buttons which, when clicked, will set the href for other buttons on the page. I have a sample code, but for some reason the target href is not being changed. When I click on "Click", then on "Target" the hi.html page is opened.

Any help would be appreciated.

Thanks, Mike


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>change href</title>

<script type="text/javascript">
function changeHref(xyz) {
    document.getElementById('myHref').href = "bye.html";
    return;
}
</script>
</head>


<body>
<a href="#" onClick="changeHref(this);">Click</a>

<br><br>

<a href="hi.html" ID="myHref">Target</a>

</body>
</html>
 
Make your function explicitly return false so the click event does not "bubble up" to the href of the "click" link of some browsers.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Tried changing to

return(false);

Still doesn't work.
 
Hi

[ul]
[li]For what Chris suggested, you also have to change the call in the event handler :
Code:
[b]<a[/b] [maroon]href[/maroon][teal]=[/teal][green][i]"#"[/i][/green] [maroon]onClick[/maroon][teal]=[/teal][green][i]"[highlight]return[/highlight] changeHref(this);"[/i][/green][b]>[/b]Click[b]</a>[/b]
[/li]
[li]What Chris suggested is an esthetic improvement, not a correction.[/li]
[/ul]


Feherke.
[link feherke.github.com/][/url]
 
As feherke points out, it should work across the board. Or at least your example code does.

If your actual code is different, I suggest looking at the error console of your browser and see if there's anything being reported, and it would be a good idea to post the actual code here so we ma take a look.

Oh, and as a note, you aren't going to see a change in the physical code of the page if you view the source from the browser.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I don't understand why, but today it worked. Must have been something stuck in the browser's cache.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top