MacSalty
MacSalty

Reputation: 1242

jQuery or JS to trim characters from inside a span

I'm trying to remove the end of some text within a span using jQuery or JS.

<h3 class="ms-standardheader ms-WPTitle">
    <a href="" tabindex="0" accesskey="W">
    <nobr>
        <span>Programs ‭[1]‬</span>

I have 15 titles that are generated like this in SharePoint - 5 Programs, 5 Clinical Services, and 5 Other Support. I want to remove the [x] from each of them, however this is auto generated by SharePoint because it doesn't like having titles of the same name.

I've tried iterations of:

$('.ms-WPTitle a nobr span').splice(0, -4);

I can't seem to get at the text within the span to trim it. Any suggestions?

Upvotes: 0

Views: 303

Answers (2)

ahren
ahren

Reputation: 16961

$('.ms-WPTitle a nobr span').text(function(el, old){ 
  return old.slice(0, -4);
});

.text() accepts a function, which you can use to easily trim chars

Demo: http://jsfiddle.net/vhTuA/

Upvotes: 5

Tats_innit
Tats_innit

Reputation: 34107

Since ahren has given one approach you can try this as well: you can also try

demo http://jsfiddle.net/CCvPQ/2/ or http://jsfiddle.net/CCvPQ/4/

API used:

Hope it fits the cause :)

code

jQuery.fn.justtext = function() {

    return $(this).children().find('span').text();

};

alert($('.ms-standardheader').justtext());

using .first api

http://jsfiddle.net/qRZe7/1/

jQuery.fn.justtext = function() {

    return $(this).children().find('span').first().text();

};

alert($('.ms-standardheader').justtext());

Upvotes: 1

Related Questions