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

href java script help required

Status
Not open for further replies.

samanthmarisetty

Programmer
Feb 28, 2008
4
US
Hi,
I am new to javascript and I wanted to know if it is possible to call java script function from a href of anchor tag.
I am doing the following in the function
<SCRIPT LANGUAGE="JavaScript">
function getString (form) {
var TestVar = "/app/sched/registration.action?username=";
var val = form.username.value;
TestVar = TestVar+val;
return TestVar;
}

<a href="getString(this.form)">1st Time User</a>

but I do not get the desired result. Is there anything that I am missing?

Please help me.

Thanks,
Samanth.
 
[1] For anchor, this.form means nothing.
[2] This is how it can be done.
[2.1] The anchor
[tt]
<a href="#" onclick="getString(this)">1st Time User</a>
[/tt]
[2.2] The handler
[tt]
function getString (obj) {
var TestVar = "/app/sched/registration.action?username=";
//if there is only one form or it is the first form which holds the data
var val = document.forms[0].username.value;
obj.href = TestVar+val;
}
[/tt]
 
Dan,

I changed to what you suggested, but this time it does not do anything. Am I missing something?

Thanks,
Samanth.
 
Suji,

Thanks a lot I think that worked. Also I do not know if this is the right place to post, is there a good book or tutorial online that I could find online for learning javascript and css?

Thanks,
Samanth.
 
[3] You can slightly elaborate it to obtain a self-sufficient miniature functional block.
[3.1] The anchor
[tt]
<a href="#" onclick="return getString(this)">1st Time User</a>
[/tt]
[3.2] The handler
[tt]
function getString (obj) {
var TestVar = "/app/sched/registration.action?username=";
//if there is only one form or it is the first form which holds the data
var val = document.forms[0].username.value;
//validation can be more elaborated
if (val.replace(/\s/g,"").length!=0) {
obj.href = TestVar+val;
return true
} else {
return false
}
}
[/tt]
[4] You can download (or it is already on your machine except you do not notice its usefulness) script56.chm from ms, excellent and concise chm. Else, everybody seems to suggest w3cschool.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top