tags:

views:

64

answers:

5

I have the following code:

<li class="zoneName"><a href="/Default.aspx?PageID=4869007">CYKF</a></li>
<li class="zoneName"><a href="/Default.aspx?PageID=4868459">YKA</a></li>

I need to add and ID to each of the < li > tags in this list. I need that ID to be the number at the end of the href string. Below is what I'd like it to be

<li class="zoneName" id="4869007"><a href="/Default.aspx?PageID=4869007">CYKF</a></li>
<li class="zoneName" id="4868459"><a href="/Default.aspx?PageID=4868459">YKA</a></li>

Any help is appreciated.

+3  A: 

Is the PageID in the anchor tag written in a server-side loop? Couldn't you modify it to write it to the <li> tag as well?

adam0101
Also, I don't believe a valid id can start with a number. You may want it to be something like id="ID4869007"
adam0101
^^^ This is true? Sorry to hijack, but I could swear I've seen plenty of integer-only query values before...
dclowd9901
Adam, Good catch on the ID. I can't change any of the server-side code. I wish I could but it's locked down really tight.
mmsa
A: 

If the href is always going to only have integers in the PageID portion of the query, and the PageID would be the only part of the query with integers:

$('li').each( function() {
    var href_val = $(this).children('a').attr('href');
    var regex = '\b[0-9]+\b';
    var new_id = new RegExp(regex, href_val);

    $(this).attr('id', new_id);
});

That's just to start you out with. Go with a more advanced regex to futureproof.

dclowd9901
A: 
$("li>a").each(function(i,el){

    var id = $(this).attr("href").split("=")[1];
    $(this).parent().attr("id", id);

})
andres descalzo
Andres, This worked perfectly. I added a bit of code to the last string to make the ID start with "zone". $("li>a").each(function(i,el){ var id = $(this).attr("href").split("=")[1]; $(this).parent().attr("id", "zone" + id);})Thanks for the quick response!
mmsa
what if there is more then one "=" in the link ? you could fix it by using the array length - 1 as selector.
meo
+1  A: 

If it must be jQuery, here you go

$("li a").each( function() {
  var match = /PageId=(\d+)$/.exec(this.href);
  if (match) $(this).parent("li")[0].id = match[1];
});
Tomalak
+1 for the regexp, is it faster to do regexp then just split?
meo
@meo: Chances are that split is faster (though we are talking academic differences here). The regex is more precise at any rate, split would work on URLs that are entirely different (false positives).
Tomalak
thx for the clarification, its just that i totally suck at writing regexp so im always happy when i can avoid them :P
meo
@meo: Regular expressions are the single most versatile and universal skill to have as a programmer. You should really pursue them, they can make life so much easier. IMHO. ^^
Tomalak
+1  A: 

you can do it like that:

demo: http://jsfiddle.net/qnEAu/

$('ul > li > a').each(function(){
    var element = $(this)
    var tempid = element.attr('href').split('=')
    element.parent('li').attr('id', tempid[tempid.length - 1])
})

note that it is not w3c valid to use numbers only as ID's

meo