Evanss
Evanss

Reputation: 23593

Hide a character in text with jQuery?

I want to change some text which says 'Name:' to 'Name'. In other words, I just want to hide the ':'.

Can jQuery select a character or word within a div? If so it would be easy to hide the ':' character or if needs be replace the text to same word but without that character.

Thanks

Upvotes: 2

Views: 8384

Answers (7)

Hamze Sheyikh Shoaei
Hamze Sheyikh Shoaei

Reputation: 823

$('#gridscroll').html($('#gridscroll').html().replace("'",""));

'hi' >> hi'

$('#gridscroll').html($('#gridscroll').html().replace(/\'/g, ""));

'hi' >> hi

Upvotes: 0

gaurav
gaurav

Reputation: 230

try bellow code it's working fine.

function removeSelectedText () {
    if (window.getSelection || document.getSelection) {
        var oSelection = (window.getSelection ? window : document).getSelection();
        $('#text').text(
            $('#text').text().substr(0, oSelection.anchorOffset).replace( $('#text').text().substr(oSelection.focusOffset),''));
    } else {
        document.selection.clear();
    }
}

$('#remove').click(function() {
    removeSelectedText();
});

Upvotes: 0

jbrookover
jbrookover

Reputation: 5150

I think the original question is whether jQuery can select based on the content of a node, not whether it can do a replace. The answer is yes; you can select by content:

$("div:contains('Name:')");

Then, you can do a simple string replace on that node, assuming that you aren't going to have other instances on the page that also have "Name:" in which you don't want to remove the colon:

var $nameDiv = $("div:contains('Name:')")
$nameDiv.html($nameDiv.html().replace(/:/g,""); // Copied from others.  I didn't check this

I'm not sure how expensive the :contains() selector is - it sounds expensive. So it might be better to wrap the key elements in a div with an ID.

Upvotes: 0

Sangeet Menon
Sangeet Menon

Reputation: 9905

If you want to use JQuery then ,

$('#idOfTheTextDIV').html($('#idOfTheTextDIV').html().replace("texttoHide",""));

Upvotes: 2

Tim B James
Tim B James

Reputation: 20364

You can use the javascript function replace.

var str = 'Name:';
str = str.replace(/:/g,'');

This will replace the ':' char with nothing basically.

Upvotes: 0

MeLight
MeLight

Reputation: 5565

You can use JavaScript's replace()

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

javaScript replace() Method

Example code :

<script type="text/javascript">

var str="Name:";
document.write(str.replace(/:/g, ""));

</script>

Upvotes: 1

Related Questions