-1

enter image description here A string contains entire value of a html.i need to get the value of href tag i.e. src value.please help me to solve.

         var alltext="a href="images/BGL30NA-10.JPG" target="""
         var str=allText;
Sriram S
  • 533
  • 3
  • 26

4 Answers4

1

if you using jQuery:

try this:

var href = $('a').attr('href')

but better if you have <a></a> with something id and then

var href = $('#youId').attr('href')
Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24
  • Gladys ,i have the html content in a string only.so help to get the string by using regx or substring – Sriram S Sep 07 '16 at 08:14
1
'a href="images/BGL30NA-10.JPG" target=""'.match(/href="(.*?)"/)[1]

try this regex .

passion
  • 1,250
  • 9
  • 15
1

hope this may help you.

try this, it will gives the value of href in javascript, below work only if you assign id attribute in your anchor tag <a href="images\images1.jpg" id="image1">

for only value of href document.getElementById("image1").getAttribute("href"); here is Jsfiddle

if you want to get full path then use this:

for full path of href document.getElementById("aaa").href; here is Jsfiddle

if you have any doubts, ask it in below comment. @sriram

Community
  • 1
  • 1
yash
  • 2,101
  • 2
  • 23
  • 32
1

Parse the string before and use appropriate DOM functions:

str = "hello, <b>my name is</b> jQuery.";
html = $.parseHTML( str );
# use $('a').attr('href') or any other selector here

The mandatory link to TONY THE PONY.

Community
  • 1
  • 1
Jan
  • 42,290
  • 8
  • 54
  • 79