2

I am having problem with fetching the text of the selected ActionLink

I am displaying the links dynamically after fetching through database like this:

 @foreach (var item in Model)
{
   <li>@Html.ActionLink(item.HobbyName, "Hobbies")</li>
}

Now, how shall I fetch the text of selected Link?

I tried this:

filename = $('a').text;
alert(filename);

But does not to work. Please help me!

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
user1274646
  • 921
  • 6
  • 21
  • 46

4 Answers4

0

Use like this

filename = $('a').text();

text() is a function, not a property.

EDIT:

If you want to get link text when you click in that link try like this

$('a').click(function() {
   filename = $(this).text();
});
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • Make sure that you're using right selector for getting anchor that you want. This example is working fine, check jsfiddle http://jsfiddle.net/4hDva/ – Chuck Norris Apr 23 '12 at 12:58
  • hi with this i am getting the text of all links in my page how shal i get the text of selected link.Please Help me – user1274646 Apr 23 '12 at 13:07
0

try this:

filename = $('li a').text();
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • hi with this i am getting the text of all links in my page how shal i get the text of selected link.Please Help me – user1274646 Apr 23 '12 at 13:07
0

@Html.ActionLink renders as standard anchor tags, so you could do something like this:

$(function(){
    $("ul li").each(function(){
       var text = $(this).find("a").text();
       alert(text);
    });        
});​

-- SEE DEMO --

Curtis
  • 101,612
  • 66
  • 270
  • 352
0
var d = document,
    arr = []

Array.prototype.forEach.call( d.getElementsByTagName( 'li' ), function ( el ) {
    arr.push( el.firstElementChild.textContent )
} )

alert( arr )

Of course this could be optimized if you set some class/id to your elements.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116