views:

26

answers:

2

Hi, this sounds really simple and stupid..but I'm having a hard time removing the content before each dash from each H4 element on my page....i've been trying to do this with Jquery/Javascript.. Any insight?

Here's a sample of my HTML code:

<h4>California-Medical Research</h4>
<h4>Florida-Industrial</h4>
<h4>Atlanta-Computers</h4>

I'm trying to have my code cycle through each occurance of <h4> and remove everything before the dash...so the desired result will look like this:

Medical Research
Industrial
Computers

Thanks!

+2  A: 

Assuming your layout's consistent with dashes you could do this:

​$("h4").text(function(i, t) { return t.substring(t.indexOf('-') + 1); });​​​​​​​​​

Since jQuery 1.4+ .text() takes a function, making this very clean. You can give it a try here.

Nick Craver
I tried this. It works, but in IE it doesn't seem to work! Any ideas?
Andrew Parisi
@Andrew - Which version of IE? The demo works here in IE8...are you sure there's not some *other* error happening, and is this running in a `document.ready` wrapper?
Nick Craver
I have just fixed the problem. Sorry for that. I was getting some "invalid character" error in IE8, and it was some hidden character that I couldn't remove until i just deleted all the whitespace around the code. Works great, thanks!
Andrew Parisi
A: 

Try this :

$.each($('h4'), function(i){
    var content = $(this).html();
    content = content.split('-')[1];
    $(this).html(content);
});
Squ36