Using Javascript, what is the method to get an element, based on the text between the opening and closing tags?

I'm a beginner, and couldn't find the answer after searching.

In my example, I'm looking for an

<a href="bla" onclick="dah">some text here</a>

I'd want to find this particular element, so I can do stuff with it.

Edit: The only thing I know that's unique about the element for sure, is the text "some text here", that's why I want to find it based on that.

preguntado el 24 de agosto de 12 a las 00:08

Can you modify the element to add an id ¿atributo? -

There is no such a method in pure JavaScript. You'd have to implement a search yourself or use a library like jQuery (as suggested in some of the the answers) -

4 Respuestas

Put id on the element:

<a href="bla" onclick="dah" id='myEl'>some text here</a>

De javascript:

var myEl = document.getElementById('myEl') // gives the element

Respondido 24 ago 12, 00:08

You can also use psuedo selector :contains, with the biblioteca jQuery.

ejemplo 2

$('div:contains("test")').css('background-color', 'red');​

http://jsfiddle.net/9z5du/

ejemplo 2

<script>
    $("div:contains('John')").css("text-decoration", "underline");
</script>

Respondido 24 ago 12, 00:08

If you know that the element is a link, you can first call getElementsByTagName [docs] to narrow down your search:

var elements = document.getElementsByTagName('a');

Then you have to iterate over the elements and test which one contains the next you are looking for:

var element = null;
for(var i = 0, l = elements.length; i < l; i++) {
    if(elements[i].innerHTML === 'some text here') {
        // found the element
        element = elements[i];
        break;
    } 
}

if(element) {
    // found the element, lets do something awesome with it
}

There are multiple ways to get the content of an element, using Node#innerText (IE) o Node#textContent (W3C) is another option. You might have to recortar the text first before you compare it.

If the HTML is as shown in your post,

if(elements[i].firstChild || elements[i].firstChild.nodeValue)

is even more elegant.

El MDN documentation about the DOM podría ser útil.

If you can modify the HTML then adding an ID and using getElementById sería la mejor opción.

contestado el 23 de mayo de 17 a las 13:05

yo usaría textContent en lugar de innerHTML if you know you are matching text. - estilo

@styfle: That's another option, but for simplicity I wanted to avoid using elements[i].textContent || elements[i].innerText. If the HTML is as shown in the post elements[i].firstChild.nodeValue sería aún mejor. - Félix Kling

Prueba esta

function getit()
{
  var elems = document.getElementsByTagName('a');

  for(var i=0; i<elems.length; i++)
  {
     var text = elems[i].childNodes[0] != null ? elems[i].childNodes[0].nodeValue : '';

     if(text == "some text here")
       doSomethingWith(elems[i]);
  }
}

Respondido 24 ago 12, 00:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.