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!!
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!!
Share Improve this question edited Sep 30, 2011 at 13:35 Matt 75.3k26 gold badges156 silver badges180 bronze badges asked Sep 30, 2011 at 13:13 Fra OreFra Ore 151 silver badge5 bronze badges 2- Can you post the code you tried? It would make pointing out the error easier. – Frédéric Hamidi Commented Sep 30, 2011 at 13:14
- What have you tried? We're happy to help but would rather not do your homework :) – Kerry Jones Commented Sep 30, 2011 at 13:14
3 Answers
Reset to default 5This 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));
});
$("a").text($("a").text().substring(0,4)+"-"+$("a").text().substring(4,6) );
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);