-1

i'm trying to add a sign (+) in my field with spinner but it return each time the number without sign, can i solve this problem somehow?

var spinner = $( "#spinner" ).spinner({
    step: 0.25,
    min:-12, 
    max:8, 
    change: function( event, ui ) {
        var ph = $('#spinner').val();
        if(ph.indexOf('+') == 0) {
          alert($('#spinner').val());
          $('#spinner').val('+'+$('#spinner').val());
       } else {

       }
    },
    spin: function( event, ui ) {

    }

});

or u can check it here:http://jsfiddle.net/XseWc/220/

2 Answers 2

1

This is for spin too: JSFIDDLE

var spinner = $("#spinner").spinner({
    step: 0.25,
    min: -12,
    max: 8,
    change: function (event, ui) {        
    },
    spin: function (event, ui) {
        var keepPlus =  spinner.val().indexOf('+') === 0?true:false;
        setTimeout(function () {
            var ph = spinner.val(); 
            if (ph > 0 && keepPlus)spinner.val('+' + spinner.val());
        }, 0);
    }

});
3
  • here it returns evrytime the plus, just waht i want is: if i write the plus it must stay when i spin else if i dont write the plus i spin without plus, but in ur case i have always plus
    – Good D
    Apr 12, 2013 at 15:28
  • cool it work!!!!thanx but i dont understand also ahy ahen i write comma in place of point the value return to 0, but when i write point evrything work fine, do u have an idea how can i fix this too ,thanx in advance
    – Good D
    Apr 12, 2013 at 15:42
  • @GoodD since replacing the point with a comma creates something that isn't a number in javascript, the plugin can't correctly detect what it is. I believe the numberFormat option or culture option can do something about this.
    – Kevin B
    Apr 12, 2013 at 15:52
1

indexOf returns an index, or -1 for not found. You need to compare with -1 rather than 0

if(ph.indexOf('+') == -1)

jsFiddle Demo

However, i suggest going about this an entirely different way. Instead, override the _format method.

$.ui.spinner.prototype._format = function(value){
    if (value > 0) {
        return "+" + value;
    } else {
        return value;
    }
}

var spinner = $( "#spinner" ).spinner({
    step: 0.25,
    min:-12, 
    max:8
});

jsFiddle Demo

Now it gets updated both on change and spin, without the ugly jumping, and it works on negative numbers too. Doing this of course means you can't use the Globalize number formatting.

18
  • 2
    To add onto this, indexOf will return "0" if a match starts on the first character of the string. "+45" would return "0" if searching for "+".
    – h2ooooooo
    Apr 12, 2013 at 15:13
  • ok it looks good, but another question why the code don't work when i spin?
    – Good D
    Apr 12, 2013 at 15:16
  • @GoodD it does, if you click off the input to trigger the change event.
    – Kevin B
    Apr 12, 2013 at 15:18
  • i mean [link]jsfiddle.net/XseWc/229 when i spin it dont change nothing, it's only working in the function change but not spin
    – Good D
    Apr 12, 2013 at 15:18
  • If you just use the spinner arrows, the plus sign is not prepended. Apr 12, 2013 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.