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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object not found - how to change the URL ?

Status
Not open for further replies.

davidku

Programmer
Aug 6, 2002
24
MY
I have the following code which got an error. It says object not found.

My objective is to change the target URL when I click on the checkbox.

Any idea whether I can access the <a href> object ?

Thanks.

----------------------------------------------------------
<script language="JavaScript">
function AssignCheckBoxURL()
{
if (frmInput.C1.checked)
frmInput.link1.href = " else
frmInput.link1.href = "}
</script>
<body>

<form method=POST name=frmInput>
<p><input type="checkbox" name="C1" value="ON" onClick="AssignCheckBoxURL()">i am happy</p>
<p><a name=link1 href=" >congratulation</a></p>
</form>
 
A link is not a form element, so it cannot be accessed under the form.

Your easiest choice would be to give the link an ID and use that to access the href of the link:

Code:
function AssignCheckBoxURL()
{
if ([red]document.[/red]frmInput.C1.checked)
[blue]document.getElementById('[ignore]link1[/ignore]')[/blue].href = "[URL unfurl="true"]http://www.happy.com";[/URL]
else
[blue]document.getElementById('[ignore]link1[/ignore]')[/blue].href = "[URL unfurl="true"]http://www.sad.com";[/URL]
}
...

[gray]<body>
<form method=POST name=frmInput>
<p><input type="checkbox" name="C1" value="ON" onClick="AssignCheckBoxURL()">i am happy</p>
<p>[/gray][blue]<a name=[ignore]link1[/ignore] [green]id="[ignore]link1[/ignore]"[/green] href="[URL unfurl="true"]http://www.happy.com"[/URL] >congratulation</a>[/blue][gray]</p>
</form>[/gray]


----------------------------------
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.
 
Thanks ... I mixed up the ID and name.

Let's say I want to enhance further and change the text as well. I need to assign an ID for the <p> tag. Correct ?
 
Hi

davidku said:
Let's say I want to enhance further and change the text as well. I need to assign an ID for the <p> tag.
Assigning [tt]id[/tt]s only helps referencing elements. If you want to change the link text it has nothing with the link's parent element.

Hint : use the [tt]a[/tt] element's [tt]innerHTML[/tt] property. This should be enough for a programmer.


Feherke.
 
Thanks for all your assistance, I will try it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top