{"id":1384,"date":"2012-11-01T17:48:11","date_gmt":"2012-11-01T17:48:11","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=1384"},"modified":"2012-11-01T17:48:11","modified_gmt":"2012-11-01T17:48:11","slug":"jquery-get-text-get-anchor-text-get-hyperlink-text-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-get-text-get-anchor-text-get-hyperlink-text-examples\/","title":{"rendered":"jQuery Get Text Examples &#8211; Get Anchor Text Hyperlink Text  &#038; more"},"content":{"rendered":"<p>Many times when working with different elements of a web page, we might feel the need to get it&#8217;s text. In this article, I am going to share a very easy way to <em><strong>get text of an anchor, a hyperlink or any other element using just 1 line code<\/strong><\/em>. Examples also follow, so read on.<!--more--><\/p>\n<h2>Examples\u00a0to Get Text of Anchor, Hyperlink, Select Dropdown, etc.<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1384\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s assume that we have few elements, such as anchor\/hyperlinks, labels, etc. on a web page. We would like to get the text of these elements. So let&#8217;s start with the basic syntax first and then we will see some specific examples.<\/p>\n<h3>Basic Syntax to get the text of any element<\/h3>\n<pre class=\"lang:js decode:true\">$(element).text();<\/pre>\n<h3>Example 1: Get Anchor Text \/ Get Hyperlink Text<\/h3>\n<p><strong>Here is our anchor\/hyperlink&#8217;s html code:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;a href=\"#\" class=\"my_link\"&gt;This is my link&lt;\/a&gt;<\/pre>\n<p><strong>To get hyperlink text, simply use the following code:<\/strong><\/p>\n<pre class=\"lang:js decode:true\">alert( $('.my_link').text() );<\/pre>\n<h3>Example 2: Get Text of Label<\/h3>\n<p><strong>Here is our label&#8217;s html code:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;label for=\"fname\" id=\"label_fname\"&gt;First Name&lt;\/label&gt;\n&lt;input type=\"text\" name=\"fname\" id=\"fname\"&gt;<\/pre>\n<p><strong>To get label text , simply use the following code:<\/strong><\/p>\n<pre class=\"lang:js decode:true\">alert( $('#label_fname').text() );<\/pre>\n<h3>Example 3: Get Text of Selected Option of a Dropdown<\/h3>\n<p><strong>Here is our Dropdown&#8217;s html code:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;select name=\"country\" id=\"country\"&gt;\n&lt;option value=\"Australia\"&gt;Australia&lt;\/option&gt;\n&lt;option value=\"London\" selected=\"selected\"&gt;London&lt;\/option&gt;\n&lt;option value=\"USA\"&gt;USA&lt;\/option&gt;\n&lt;\/select&gt;<\/pre>\n<p><strong>To get <strong>selected option\u00a0<\/strong>text of the dropdown, simply use the following code:<\/strong><\/p>\n<pre class=\"lang:js decode:true\">alert( $('#country option:selected').text() );<\/pre>\n<p><strong>To get the text of a specific option of the dropdown:<\/strong><\/p>\n<p>Here, we would like to get the text of the third option regardless of whether it is selected or not.<\/p>\n<pre class=\"lang:js decode:true\">alert( $('#country option:eq(2)').text() );<\/pre>\n<p>In the above code, note that we want to get the text of the 3rd option, so we have to use eq(2) &amp; <strong>NOT<\/strong> eq(3) as indexing starts at 0. So if you want to get the text of the first option, then you would use:<\/p>\n<pre class=\"lang:js decode:true\">alert( $('#country option:eq(0)').text() );<\/pre>\n<p><strong>To get the text of an option of the dropdown, based on its value:<\/strong><\/p>\n<p><strong><\/strong>Let&#8217;s assume that you want to get the text of the &#8216;USA&#8217; option. It can be done like so:<\/p>\n<pre class=\"lang:js decode:true\">alert( $('#country option[value=\"USA\"]').text() );<\/pre>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=1384\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>That&#8217;s it for now!<\/h5>\n<p>Do you know of any other ways to <em><strong>get anchor text \/ hyperlink text or text of any element using jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many times when working with different elements of a web page, we might feel the need to get it&#8217;s text. In this article, I am going to share a very easy way to get text of an anchor, a hyperlink or any other element using just 1 line code. Examples also follow, so read on.<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[40,42,41],"class_list":["post-1384","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-2","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-mk","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=1384"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/1384\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}