Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to reference text in javascript function

Posted on 2007-12-04
9
229 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
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…

838 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