0

how to get match (id) by jquery I want to get (id) in (href) Between (span)

If the code here

HTML

<table>
<tr>
<td class='zero'>
  <span>
   <a href="http//google&id=222.com">123</a>
   <a>hgi</a>
  </span>
   <span>
   <a href="http//google&id=111.com">123</a>
   <a>hgi</a>
  </span>
   <span>
   <a href="http//google&id=333.com">123</a>
   <a>hgi</a>
  </span>

</td>
</tr>
</table>

jquery

$(".zero span").each(function(){
$(this).find("a").attr("href").match(/id=/);

});

but Error >_< why ? and how ?

i want

id=222
id= 333
id= 111
hokozere
  • 13
  • 3
  • if it's dynamically generated why not just use an actual ID, i.e. link, then target that? – Matt Saunders Aug 31 '14 at 15:29
  • If the URLs are sufficiently varied and complicated, you might want to try using the deparam function [referenced here](http://stackoverflow.com/questions/1131630/the-param-inverse-function-in-javascript-jquery) instead of writing your own – iabw Aug 31 '14 at 15:54

3 Answers3

1

Some of your <a /> elements do not have an href property. attr() will return undefined in this case. When trying to call match() on undefined, JavaScript throws an error.

Fix it with:

$(".zero span").each(function(){
    href = $(this).find("a").attr("href");
    if (href !== undefined) {
        href.match(/id=/);
    }
});

Or change the selector to include a[href].

Outside:

$(".zero span a[href]").each(function(){
    $(this).attr("href").match(/id=/);        
});

Inside:

$(".zero span").each(function(){
    $(this).find("a[href]").attr("href").match(/id=/);
});
naneau
  • 489
  • 2
  • 4
0

You need this:

$(".zero span").each(function(){
   alert($(this).find("a").attr("href").match(/id=\d+/)[0]);
});
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

Have you tried

$(this).find("a").attr("href").match(/id=.*/);

That will match id and all characters after.

Scientaster
  • 102
  • 6