Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get link text using jQuery?

How can I get link text using jQuery?

My link is:

<a href="htp://www.google.com" id="id1">google site</a>

I need to get "google site" using jQuery.

like image 337
user3000967 Avatar asked Mar 16 '14 16:03

user3000967


People also ask

How to get input text in jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.

How can I get the text value of a clicked link?

Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

How do I change a href text?

Change an existing hyperlinkRight-click anywhere on the link and, on the shortcut menu, click Edit Hyperlink. In the Edit Hyperlink dialog, select the text in the Text to display box.

How use contains in jQuery?

The :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).


1 Answers

That is the text shown to the user in UI not the name. To get that you use a jQuery's text method.

Use the .text() method as

$('a').text();

Even you can use a variable to get the value to, for example:

var hyperLinkText = $('a').text(); // get the name
$('element').html(hyperLinkText); // write it somewhere
like image 169
Afzaal Ahmad Zeeshan Avatar answered Oct 06 '22 00:10

Afzaal Ahmad Zeeshan