Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force an event in jQuery

To all;

I have created a up and down counter for decimal places and when a change occurs I have it force a blur event to recalculate fields with the following code:

$('button').click(function(){       
    var decPlaces = document.calculator.dpv.value * 1;
    var hii = document.calculator.origin.value;
    if (this.id == 'up' && decPlaces < 9){                    
        document.calculator.dpv.value  =  decPlaces + 1;
        if (hii != ''){
            document.calculator[hii].focus();
            document.calculator[hii].blur();
        }
    }
    if (this.id == 'down' && decPlaces > 0){    
        document.calculator.dpv.value  =  decPlaces - 1;
        if (hii != ''){
            document.calculator[hii].focus();
            document.calculator[hii].blur();
        }
    } 

Works well in FF but drags in others particularly IE - suggestions on making the cleaner and faster is appreciated.

Bob

like image 733
Bob Knothe Avatar asked Aug 09 '09 15:08

Bob Knothe


2 Answers

The official jquery way to trigger/force an event is

$("selector").trigger("blur");
$("selector").trigger("focus");

But I'm not sure this is what will help you.

like image 149
Julian de Wit Avatar answered Nov 07 '22 19:11

Julian de Wit


You're mixing jQuery and DOM calls, you should really avoid doing that.

Create specific handlers for the Down and Up buttons (by using either ID tags or class tags) and then change the value of your calculator value by calling the jQuery $("#calculator").val(decPlaces + 1);

like image 27
CMerat Avatar answered Nov 07 '22 18:11

CMerat