I have a variable that reads as _123456 and I need to delete the underscore prefix on this variable before storing it to a field. How do I do this?
var value = "_123456"
I have a variable that reads as _123456 and I need to delete the underscore prefix on this variable before storing it to a field. How do I do this?
var value = "_123456"
This is just generic Javascript, not specific to jQuery. You'd do something like this:
var result = value.substring(1);
Or...
var result = value.replace(/^_/, '');
Or... (if there can be more than one underscore)
var result = value.replace(/^_+/, '');
[not recommended] To do this with jQuery (and an if):
var element = $('<div></div>');
var text = "_123456";
for(var i = 0; i < text.length; i++) {
if ( i > 0 ) {
element.append('<span>' + text[i] + '</span>');
}
}
var trimmed = element.text();
I tried element.remove(':first')
instead of the if
, but it didn't seem to work. No idea why.
FYI - If the underscore is after the digits then you could use parseInt()
var value = "123456_"
So for example parseInt("123456_")
will return the number 123456.