Fra Ore
Fra Ore

Reputation: 15

jQuery change text in link

I have a simple question for jQuery...

I have a table with a link like this

<table>
  <tr>
    <td class="views-field">
      <a href="ciao">201105</a>
    </td>
  </tr>
</table>

Now I would change the link's text from 201105 to 2011-05

(simple add a "-" after the first 4 characters)

I tried substring but don't work... Help me!!

Upvotes: 0

Views: 4731

Answers (3)

Bas Slagter
Bas Slagter

Reputation: 9929

Try the code below, that should work fine.

var replaceElement = $("td.views-field a").first();
var oldDate = replaceElement.text();

var newDate = oldDate.substring(0, 4) + "-" + oldDate.substring(4,6);
replaceElement.text(newDate);

Upvotes: 0

Alan
Alan

Reputation: 652

$("a").text($("a").text().substring(0,4)+"-"+$("a").text().substring(4,6) );

Upvotes: 0

nachito
nachito

Reputation: 7035

This will translate all td.views-field links:

$('td.views-field a').each(function () {
  var oldText = $(this).text();
  $(this).text(oldText.substr(0,4) + '-' + oldText.substr(4));
});

Upvotes: 5

Related Questions