Mark
asked on
how to reference text in javascript function
I have a link:
<a href="javascript:doSubmit( 'the rain in spain',myParameter);">the rain in spain</a>
how can I reference my text string without typeing it twice. I want something like:
<a href="javascript:doSubmit( this.value ,myParamet er);">the rain in spain</a>
(which I haven't actually tried, btw - so if that is correct, snag yourself some free points)
<a href="javascript:doSubmit(
how can I reference my text string without typeing it twice. I want something like:
<a href="javascript:doSubmit(
(which I haven't actually tried, btw - so if that is correct, snag yourself some free points)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
flipz, close but sorry, your solution gave "undefined". Personally, I think your solution *should* work, but alas
nizsmo - that worked! Thanks.
nizsmo - that worked! Thanks.
erm....jmarkfoley:
did you get our names back to front? or did you just accept the solution that didn't work?
did you get our names back to front? or did you just accept the solution that didn't work?
No objections here - nizsmo should be awarded the points.
Hi, just to clarify:
the reason why "this.innerHTML" will return "undefined" in flipz's example ist that *inside the href* of the link, "this" refers to the *window element*, not to the link. Easy to test - here
<a href="javascript:alert(thi s);">Who is "this"?</a>
Firefox will alert "[object Window]", and this
<a href="javascript:alert(thi s.location );">Who is "this"?</a>
will alert the window's location.
I would do it differently - use the onClick instead of the href for the javascript (it's always a good idea). "This" will work as expected here:
<a href="#" onClick="doSubmit(this.inn erHTML, myParameter); return false;">the rain in spain</a>
(you might not need "return false")
the reason why "this.innerHTML" will return "undefined" in flipz's example ist that *inside the href* of the link, "this" refers to the *window element*, not to the link. Easy to test - here
<a href="javascript:alert(thi
Firefox will alert "[object Window]", and this
<a href="javascript:alert(thi
will alert the window's location.
I would do it differently - use the onClick instead of the href for the javascript (it's always a good idea). "This" will work as expected here:
<a href="#" onClick="doSubmit(this.inn
(you might not need "return false")
ASKER
nizmo: sorry, I must have accidentally selected the wrong answer. I'll fix that now.
TName, your idea is pretty good too and I may keep it in mind for the future, but nizmo beat you to the punch this time and I've already implemented his idea.
Thanks all, sorry for messing up.
TName, your idea is pretty good too and I may keep it in mind for the future, but nizmo beat you to the punch this time and I've already implemented his idea.
Thanks all, sorry for messing up.
no problem thanks flipz and jmarkfoley :)
Open in new window