I have a string
<a href="http://google.com">http://google.com</a> This is a good page
I try to get only http://google.com but i can't.
Please give me some solution to do it, thank you.
I have a string
<a href="http://google.com">http://google.com</a> This is a good page
I try to get only http://google.com but i can't.
Please give me some solution to do it, thank you.
Try this
Get the value :
$('a').attr('href');
Replace the link :
$("a").attr('href','javascript:void(0)');
Get the text :
$("a").text();
First, you have to convert the string to DOM elements, using one of the techniques in this question. (I use the accepted answer, but you should choose the one that matches your use-case the best.)
var str = '<a href="http://google.com">http://google.com</a> This is a good page';
var div = document.createElement('div');
div.innerHTML = str;
You can then manipulate the elements using normal JavaScript DOM methods. Here I do it with jQuery:
$(div).text(); // "http://google.com This is a good page"
$('a', div).attr('href'); // "http://google.com"
A final note: beware when working with with HTML strings! If you put untrusted HTML into your page's DOM, you will create a cross-site scripting vulnerability. If you control the source of the JSON data, then it would be a good idea to just send the strings you need, instead of HTML strings - that way you can avoid parsing the HTML altogether.
Try following js code,
following code is working but not recommended,
$('a').attr('href');
$("a").attr('href','javascript:void(0)');
Get the text :
$("a").text()