?
Solved

how to reference text in javascript function

Posted on 2007-12-04
9
Medium Priority
?
253 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
[X]
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
  • 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 2000 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
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…
Suggested Courses

752 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