tags:

views:

427

answers:

4

I want to remove first character from link's text with jQuery.

<span class="test1"> +123.23 </span>
<span class="test2"> -13.23 </span>

I want to remove the "+" and "-" from with jQuery.

Output:

<span class="test1"> 123.23 </span>
<span class="test2"> 13.23 </span>
+4  A: 
var val = $("span").html();
$("span").html(val.substring(1, val.length);
ss
Downvoted a step back: 1) this replaces the value of all span elements with the value of the first span element. 2) this example has a syntax error.
BalusC
Your starting index is incorrect. Should be 2. Also, you need to add a leading space to the new value since that's what's needed in the output.
Chris Williams
+5  A: 
$("span.test1, span.test2").each(function() {
  $(this).text($(this).text().replace(/[+-]/, ""));
});
cletus
+1 substring is the bad way of doing this. Despite the author's answer tick.. this answer using Regex is the right answer!
Evildonald
Awesome, good idea !
Alexander Corotchi
@Evildonald: why substring is bad ? I agree that it's better to use Regex, but I won't call substring bad :-)
Soufiane Hassou
Why is Regex better? If you know where the substitution needs to happen, substring is better than Regex from both a performance and readability standpoint.
Chris Williams
+2  A: 

you can get/set the HTML using .html() and remove the first character using .substring(), I think it's pretty clear now, you just need to write a 2 (or 3) lines code.

Soufiane Hassou
+2  A: 
// get the current text
text1 = $(".test1").html();
// set the text to the substring starting at the third character
$(".test1").html(text1.substring(2)); // extract to the end of the string

text2 = $(".test2").html();
$(".test2").html(" " + text2.substring(2)); // looks like you want to keep the leading space
Chris Williams