1

I'd like to select a specific text of an anchor and modify it at it end. I have many unordered lists on a page and I'd like to get a text of an anchor that has a specified href. here is an example:

<ul>
   <li><a href="vue.php?view_id=6">vue 6</a></li>
   <li><a href="vue.php?view_id=7">vue 7</a></li>
</ul>
<ul>
   <li><a href="vue.php?view_id=8">vue 8</a></li>
   <li><a href="vue.php?view_id=9">vue 9</a></li>
</ul>

What if I'd like to get the text of vue 6 for example, using jquery?

djasy3
  • 25
  • 7
  • Show us what you have tried in `AJAX` – John R Feb 23 '16 at 17:13
  • sorry it is jquery ! let me modify that ! – djasy3 Feb 23 '16 at 17:25
  • You want this specific anchor text following what? User interaction as click? Depending href attribute? Depending index? Or what??? – A. Wolff Feb 23 '16 at 17:29
  • @A.Wolff: the point was, someone can click on the text of the anchor and get a modal window that will come with other information on an edit form. the text of the anchor is just the title. After modifying the text and closing the modal window I wanted the effect to take directly on the page as I'm using ajax. so on the background I have already the info on href. – djasy3 Feb 23 '16 at 17:39
  • If it is following user interaction, then inside relevant event handler (e.g click one), `this` would refer to clicked element. But your last comment is quite vague regarding your expected behaviour. It seems you have find the right answer below but i'm not sure this is really what you should use, depending what is your exact use case – A. Wolff Feb 23 '16 at 17:43
  • Possible duplicate of [how i can select this element with Jquery?](http://stackoverflow.com/questions/5561711/how-i-can-select-this-element-with-jquery) – James Feb 23 '16 at 19:07

3 Answers3

0

$('a[href="vue.php?view_id=6"]')

ken4z
  • 1,340
  • 1
  • 11
  • 18
0

Here you have the code to play with. Basically is something like this:

Get the Text:

$("ul li a[href='vue.php?view_id=9']").text();

Change the Text:

$("ul li a[href='vue.php?view_id=9']").text("My new text");

Just change the value 'vue.php?view_id=9' for the link you need.

epinal
  • 1,415
  • 1
  • 13
  • 27
0

I'd like to select a specific text of an anchor and modify it at it end.

feel the contains() method is pretty useful in this case:

$(function() {
 var $el = $(":contains(vue 6)"),
    text = $el.text(),
    mod = text + " Modification";

  $el.text(mod); // making modification
})

Note: corrected initial response, simplified example code for demonstration

Todd
  • 5,314
  • 3
  • 28
  • 45