• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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,myParameter);">the rain in spain</a>

(which I haven't actually tried, btw - so if that is correct, snag yourself some free points)
0
jmarkfoley
Asked:
jmarkfoley
  • 3
  • 2
  • 2
  • +1
1 Solution
 
flipzCommented:
Try this - just had to implement a similar solution yesterday although I haven't tested this one!
<a href="javascript:doSubmit(this.innerHTML,myParameter);">the rain in spain</a>

Open in new window

0
 
nizsmoDeveloperCommented:
<a href="javascript:doSubmit(document.getElementById('test').innerHTML,myParameter);" id="test">the rain in spain</a>

This should work.
0
 
jmarkfoleyAuthor Commented:
flipz, close but sorry, your solution gave "undefined". Personally, I think your solution *should* work, but alas

nizsmo - that worked! Thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nizsmoDeveloperCommented:
erm....jmarkfoley:

did you get our names back to front? or did you just accept the solution that didn't work?
0
 
flipzCommented:
No objections here - nizsmo should be awarded the points.
0
 
TNameCommented:
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(this);">Who is "this"?</a>
Firefox will alert "[object Window]", and this
    <a href="javascript:alert(this.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.innerHTML, myParameter); return false;">the rain in spain</a>

(you might not need "return false")
0
 
jmarkfoleyAuthor Commented:
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.
0
 
nizsmoDeveloperCommented:
no problem thanks flipz and jmarkfoley :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now