Solved

how to reference text in javascript function

Posted on 2007-12-04
9
213 Views
Last Modified: 2010-04-09
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
Comment
Question by:jmarkfoley
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 11

Expert Comment

by:flipz
ID: 20406265
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
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20406286
<a href="javascript:doSubmit(document.getElementById('test').innerHTML,myParameter);" id="test">the rain in spain</a>

This should work.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20406534
flipz, close but sorry, your solution gave "undefined". Personally, I think your solution *should* work, but alas

nizsmo - that worked! Thanks.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20406583
erm....jmarkfoley:

did you get our names back to front? or did you just accept the solution that didn't work?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Expert Comment

by:flipz
ID: 20412389
No objections here - nizsmo should be awarded the points.
0
 
LVL 28

Expert Comment

by:TName
ID: 20413339
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
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20413749
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
 
LVL 21

Expert Comment

by:nizsmo
ID: 20415592
no problem thanks flipz and jmarkfoley :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now