Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use jQuery to select text inside anchor tags
    primarykey
    data
    text
    <p>I want to select the <code>a</code> tag and display its text when clicked.</p> <p>In another word, when I click the first link - "One", I want to display its text "One" using <code>alert</code>.</p> <p>When I click the second link - "Example", I want to display the text "Example" using <code>alert</code>.</p> <pre><code>&lt;body&gt; &lt;div id="tree"&gt; &lt;ul&gt; &lt;li&gt;&lt;a target="_blank" href="one.html"&gt;One&lt;/a&gt;&lt;/li&gt; &lt;li class="folder expnded"&gt;&lt;a target="_blank" href="two.html"&gt;Examples&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div id="display"&gt;&lt;/div&gt; &lt;/body&gt; </code></pre> <p><strong>Update 1:</strong></p> <p>Thank everyone for answering. What I really want to do is that I need to create a tree structure and when I click tree leaf node, I have to display name of that leaf node.</p> <p>I created tree structure using jQuery DynaTree, but jQuery selectors is not working for me in the code above.</p> <p>I'm not able to select tags or any other elements inside the <code>div</code> tag.</p> <p>Below is the tree structure:</p> <p><img src="https://i.stack.imgur.com/VWX8R.png" alt="enter image description here"></p> <h2>THIS IS MY FULL HTML CODE (above is just a sample code)</h2> <pre><code>&lt;html&gt; &lt;head&gt; &lt;!-- Include the required JavaScript libraries: --&gt; &lt;script src="js/jquery-1.7.1.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src='js/jquery-ui-1.8.20.custom.min.js' type="text/javascript"&gt;&lt;/script&gt; &lt;script src='js/myjquery.js' type="text/javascript"&gt;&lt;/script&gt; &lt;link rel="stylesheet" type="text/css" href="css/ui.dynatree.css"&gt; &lt;link rel="stylesheet" type="text/css" href="css/style.css"&gt; &lt;script src="js/jquery.dynatree.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="tree"&gt; &lt;ul&gt; &lt;li&gt;one&lt;/li&gt; &lt;li&gt;&lt;a target="_blank" href=""&gt;Google&lt;/a&gt; &lt;li class="folder expnded"&gt;Examples &lt;ul&gt; &lt;li&gt;&lt;a target="content" href="" id="s"&gt;one&lt;/a&gt; &lt;li&gt;&lt;a target="content" href="two.html"&gt;two&lt;/a&gt; &lt;li class="folder"&gt;Folder one &lt;ul&gt; &lt;li&gt;&lt;a target="content" href="one.html"&gt;one&lt;/a&gt; &lt;li&gt;&lt;a target="content" href="two.html"&gt;two&lt;/a&gt; &lt;/ul&gt; &lt;li&gt;&lt;a target="content" href="three.html"&gt;three&lt;/a&gt; &lt;li&gt;&lt;a target="content" href="four.html"&gt;four&lt;/a&gt; &lt;li class="folder"&gt;Folder two &lt;ul&gt; &lt;li&gt;&lt;a target="content" href="one.html"&gt;one&lt;/a&gt; &lt;li&gt;&lt;a target="content" href="two.html"&gt;two&lt;/a&gt; &lt;/ul&gt; &lt;li class="folder"&gt;Folder three &lt;ul&gt; &lt;li&gt;&lt;a target="content" href="one.html"&gt;one&lt;/a&gt; &lt;li&gt;&lt;a target="content" href="two.html"&gt;two&lt;/a&gt; &lt;li class="folder"&gt;Inner Folder &lt;ul&gt; &lt;li&gt;&lt;a target="content" href="one.html"&gt;one&lt;/a&gt; &lt;li&gt;&lt;a target="content" href="two.html"&gt;two&lt;/a&gt; &lt;/ul&gt; &lt;/ul&gt; &lt;/ul&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div id="display"&gt; &lt;a href="" id="s"&gt;one&lt;/a&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The picture I posted is the output of above HTML code.</p> <p>In myjquery.js file I have code (old) like this</p> <pre><code>$(function() { // --- Initialize sample trees $("#tree").dynatree({ autoFocus : true, // persist: true, minExpandLevel : 2, onFocus : function(node) { // Auto-activate focused node after 1 second if (node.data.href) { node.scheduleAction("activate", 1000); } }, onBlur : function(node) { node.scheduleAction("cancel"); }, onActivate : function(node) { if (node.data.href) { window.open(node.data.href, node.data.target); } } }); }); </code></pre> <p><strong>Update 2:</strong> </p> <h2>solution for my problem</h2> <p>In myjquery.js file, the following new code works for me. You can compare it with old code above.</p> <pre><code>$(function() { // --- Initialize sample trees $("#tree").dynatree({ autoFocus : true, persist: true, minExpandLevel : 1, onFocus : function(node) { // Auto-activate focused node after 1 second if (node.data.href) { node.scheduleAction("activate", 1000); } }, onBlur : function(node) { node.scheduleAction("cancel"); }, onActivate : function(node) { if (node.data.href) { window.open(node.data.href, node.data.target); } }, onClick : function(node) { $.ajax({ type : "GET", url : "Display", data : { id : node.data.title }, success : function(data) { $("#display").html(data); } }); } }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload