0

Is there a way to get code behind a content in jQuery, like <a href="http://www.example.com" class="link">Text</a>

So i need something like: if ($(".link").text() == "Text"){alert("<a href="http://www.example.com" class="link">Text</a>");}

The thing is that i need to get the text from alert(); from page, and maybe some code before.

isherwood
  • 58,414
  • 16
  • 114
  • 157
Radu Dascălu
  • 318
  • 4
  • 15

4 Answers4

0

You can use contains selector:

if($('.link:contains("Text")')){
  alert("Text");
}
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

If it is a link like this:

<a href="#" class="link">Text</a>

You can do

if ($(".link").html()=="Text") {
    alert("Text");
}
Leggy
  • 682
  • 7
  • 20
0

Get the html for the element with an id of my-link:

$("#my-link")[0].outerHTML

working example: http://jsfiddle.net/jwnace/57en1v9s/

Get the html for the element with the text of sample text:

$("a:contains('sample text')")[0].outerHTML

working example: http://jsfiddle.net/jwnace/z3ymh84c/

jwnace
  • 360
  • 3
  • 11
0

Given this link:

<a href="http://www.example.com" class="link">Text</a>

... you can get its HTML like so:

var myLink = $('a:contains("Text")')[0].outerHTML;

... and then do with it what you will.

alert(myLink);

Demo

Read more


Here's how you'd get the anchor's parent element as well:

var myLink = $('a:contains("Text")').parent()[0].outerHTML;

Demo


Aaaaaaand, to add a class to the outer element, you could do this:

var myLink = $('a:contains("Text")').parent();

$(myLink).addClass('my-class');

var myLinkHtml = $(myLink)[0].outerHTML;

Demo

Community
  • 1
  • 1
isherwood
  • 58,414
  • 16
  • 114
  • 157