jquery - How to extract all hyperlink titles from big html string using javascript? -


i got html string :var code; want extract hyper link title values in big string , place them in textarea. tried following never works. 1 tell me doing wrong?

sample hyperlinks for(i want extract mango,cherry,...) :

 <a href="/season/">mango</a>  <a href="/season/">cherry</a> 

my code string has blocks of data below:

<div class="details">     <div class="title">       <a href="/season/">mango</a>       <span class="type">3</span>     </div>    </div> 

full code:

$.getjson('http://anyorigin.com/get?url=http://asite.com/getit.php/&callback=?', function(data){      //$('#output').html(data.contents);   var sitecontents = data.contents;      //writes textarea   document.myform.outputtext.value = sitecontents ;   var start = sitecontents.indexof('<ul class="list">'); var end = sitecontents.indexof('<ul class="pag">', start); var code = sitecontents.substring(start, end);  document.myform2.outputtext2.value = code ;   var pattern = /<a href="([^"]+?)">([^<]+?)<\/a>/gi;     code = code.match(pattern);     (i = 0; < code.length; i++) {         document.write($2<br />'));     }  });  </script> 

it looks you're trying parse html regex. this post has more info on topic.

since question tagged jquery, try following...

make jquery object out of returned html:

$markup = $(data.contents); 

find anchors:

$anchors = $markup.find('a'); 

get text (or whatever attribute want it):

arrtext = []; $anchors.each(function() {     arrtext.push($(this).text()); }); 

put result textarea:

$textarea.val(arrtext.join(',')); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -