BrownChiLD
BrownChiLD

Reputation: 3733

jQuery selecting a character inside a string

So i have a div that contains the text "12:00PM" .. it's a little clock application.

<div id="#clock">12:00PM</div>

I wanted to make the ":" part disappear and then reappear..

I wonder if this was possible w/ jQuery.

i have all the intervals and everything worked out... my clock gets updated.. but i just need a way to target/select that ":" character.. and remove it.. and then replace it :)

Is this possible without being too complicated?

Thanks

Upvotes: 1

Views: 281

Answers (3)

kei
kei

Reputation: 20521

DEMO

HTML is unchanged

CSS

.hidden {
    visibility:hidden;
}

jQuery

$clock = $("#clock");
$blink = true;
setInterval(function(){
    if ($blink) {
        $clock.html($clock.html().replace(":",'<span class="hidden">:</span>'));
        $blink = false;
    }
    else {
        $clock.html($clock.html().replace('<span class="hidden">:</span>',":"));
        $blink = true;
    }
}, 1000);

Upvotes: 2

user1710796
user1710796

Reputation:

If it's always going to be the same character, use the search() or indexOf() methods and then substring() and concatenation to eliminate the character.

Upvotes: 1

Phil
Phil

Reputation: 11175

Html:

<div id="#clock">12<span id="clock-colon">:</span>00PM</div>

jQuery:

$('#clock-colon').hide() // do what you want

Upvotes: 2

Related Questions