tags:

views:

736

answers:

4

I am trying to remove the last character from a div id using jquery. Currently I have;

    <div id="foo/"></div>

But I need ;

    <div id="foo"></div>

The character is always a '/' and is generated automatically (re:annoyingly) by the cms I am using, having the '/' there is messing up some javascript linking I am trying to do.

Any help on how to remove or resolve this would be massively appreciated!

Thanks in advance

A: 

try:

$("div").each(function(){
    var i=$(this).attr("id");
    if(i){
         i = i.substr(0,i.length-1);
         $(this).attr("id",i);
    }
});
TheVillageIdiot
There could be 100 divs that are fine on the page. It would be more efficient to use a more detailed jQuery selector so you end up looking through less items.
Doug Neiner
It was just to show him a way to do it.
TheVillageIdiot
+3  A: 

You can select all the divs that have an id attribute, and check if the / character is present at the end, and then remove it:

$('div[id]').each(function(){
    var id = $(this).attr('id');
    if (id.indexOf('/') == id.length-1) {
      $(this).attr('id', id.slice(0, -1));
      // or $(this).attr('id', id.substring(0, id.length-1));
    }
});
CMS
You really should use `id$=/` to match only those items. And `$(this).attr('id')` should be `this.id`. Creating a new jQuery object and calling a function `attr` is not the best/fastest way of getting/editing the id.
Doug Neiner
@dcneiner, I thought about that, but since the `/` character is not valid for the ID attribute, he might have unexpected behavior on different browsers by using that selector... http://is.gd/536C6
CMS
I realize it is invalid, but jQuery is treating it as any other attribute at that point, and the `/` didn't need to be escaped in my tests, though it still worked as expected.
Doug Neiner
This also worked perfectly, thank you so much for all your help!
Robbie White
A: 

This might do it:

$('*').each(function(){
  $(this).attr('id', $(this).attr('id').replace(/\/$/, ''));
});

Replace the * with div or any css selector supported by jquery.

samg
+6  A: 

Its easy by using the attribute$= CSS3 selector which means "ends with" in your jQuery selection:

$("div[id$=/]").each(function(){ 
    this.id = this.id.substr(0,this.id.length - 1);
});

Edit: Using Adobe® Browser Lab, I tested this code in IE6, IE7, Firefox 2.0 and Firefox 3.0, Safari 3.0. I have also tested locally in Safari 4.0 and Firefox 3.5. In all browsers it worked properly, and removed the trailing / from the id.

Doug Neiner
+1 nice, is that also mean, ^ is startswith and * means include?
S.Mark
Yes it does. It can handle most of the CSS3 selectors, plus jQuery adds a few more.
Doug Neiner
cool, I found this http://www.w3.org/TR/css3-selectors/ too, I should give it a try.
S.Mark
Worked wonderfully! Thank you so much for such and elegant little piece of code! That CSS ends with selector is something I didn't even know existed! Epic find :) Thanks again!
Robbie White