Solved

how to reference text in javascript function

Posted on 2007-12-04
9
222 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

831 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