473,388 Members | 1,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

getting value of a hyperlink from within one

Hi,

How would I use Javascript to get the value of a hyperlink. That is,
what function could I substitute below to get the value "2"?

<a href="javascript:alert( /* WHat goes here? */ );">2</a>

Thanks for the help, - Dave

Jun 17 '06 #1
5 6243
Hi,

la***********@zipmail.com wrote:
Hi,

How would I use Javascript to get the value of a hyperlink. That is,
what function could I substitute below to get the value "2"?

<a href="javascript:alert( /* WHat goes here? */ );">2</a>

Thanks for the help, - Dave


<a href="#"
onclick="alert( this.firstChild.nodeValue );return false;">2</a>

Note: that code works in IE and Mozilla, but it breaks in Mozilla if you
add a carriage return between the '>' and the '2'. If you do, then the
firstChild is a text node containing the carriage return, so you must
then modify your code accordingly.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch
Jun 17 '06 #2
la***********@zipmail.com wrote:
Hi,

How would I use Javascript to get the value of a hyperlink. That is,
what function could I substitute below to get the value "2"?

<a href="javascript:alert( /* WHat goes here? */ );">2</a>


Using the javascript pseudo protocol as the value of an href attribute
is not a good idea, use a real URL and onclick. If you really don't
want the href to do anything, then don't use an A element, use a span or
div styled to look like a link.

If there is no HTML inside the A element, then:

<a href="#" onclick="alert(this.firstChild.data);">2</a>
If the A element will contain HTML, say <b> or <i> elements, you may
want to use IE's innerText and the W3C textContent properties of the A
element (will IE 7 support textContent?). A cross-browser example:

function getElText(el)
{
if (el.textContent) return el.textContent;
if (el.innerText) return el.innerText;
var x = el.childNodes;
var txt = '';
for (var i=0, len=x.length; i<len; ++i){
if (3 == x[i].nodeType) {
txt += x[i].data;
} else if (1 == x[i].nodeType){
txt += getElText(x[i]);
}
}
return txt.replace(/\s+/g,' ');
}
--
Rob
Jun 18 '06 #3
On Sun, 18 Jun 2006 10:37:53 +1000, RobG wrote:
la***********@zipmail.com wrote:
Hi,

How would I use Javascript to get the value of a hyperlink. That is,
what function could I substitute below to get the value "2"?

<a href="javascript:alert( /* WHat goes here? */ );">2</a>


Using the javascript pseudo protocol as the value of an href attribute
is not a good idea, use a real URL and onclick. If you really don't
want the href to do anything, then don't use an A element, use a span or
div styled to look like a link.


Why is this a bad idea - is it deprecated?
Jun 18 '06 #4
andy baxter said the following on 6/17/2006 8:48 PM:
On Sun, 18 Jun 2006 10:37:53 +1000, RobG wrote:
la***********@zipmail.com wrote:
Hi,

How would I use Javascript to get the value of a hyperlink. That is,
what function could I substitute below to get the value "2"?

<a href="javascript:alert( /* WHat goes here? */ );">2</a>

Using the javascript pseudo protocol as the value of an href attribute
is not a good idea, use a real URL and onclick. If you really don't
want the href to do anything, then don't use an A element, use a span or
div styled to look like a link.


Why is this a bad idea - is it deprecated?


<URL: http://jibbering.com/faq/#FAQ4_24>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 18 '06 #5
Randy Webb wrote:
Why is this a bad idea - is it deprecated?

<URL: http://jibbering.com/faq/#FAQ4_24>


And also
http://www.javascripttoolbox.com/bestpractices/#onclick

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ray | last post by:
I've got a Database Results Wizard form with a hyperlink which passes data onto the next page. I wish to send two pieces of data along this link. The first item is from within the DRW data, so I...
1
by: Matt | last post by:
For example, how to represent hyperlink in XSL? I want to add hyperlink in XSL. i.e. I need to generate <A HREF="http://mypage.html">home page</A> in HTML. I tried the following approaches but...
1
by: Robert | last post by:
This is a repost from the other dotnet.csharp forum: I'm including a simple datagrid control on my web form. I have created a Dataview (reportListView) onto a data table to populate it. At its...
2
by: John | last post by:
Hi I am trying to save the value of HTTP_REFERER so I can use it later on in the webform as below; Public Return_Address as string Private Sub Page_Load(ByVal sender As System.Object,...
1
by: Daniel Michaeloff | last post by:
Hi all, I have an application that when finished redirects to a non-ASP.NET app which is choking on a huge ASP.NET session cookie. The cookie "ASP.NET_SessionId" gets transmitted by the browser...
3
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are...
0
by: Seok Bee | last post by:
Dear Experts, I am having a wizard control in my web app. I have some textbox controls within the wizard control. At the same time, I have a hyperlink to run a pop-up window for calendar. The...
3
tdw
by: tdw | last post by:
I have a working hyperlink field in a table, and it's form. I also have a working File Picker Dialog: FileDialog(msoFileDialogFilePicker), being used to create the hyperlink. What I need to do, and...
2
by: ismailc | last post by:
Hi, I need help please. I have a page with a stylesheet that is read from an aspx form. On the form I have an hyperlink object - which iIwant the Targeturl to open within the current frame where...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.