Michael Falciglia
Michael Falciglia

Reputation: 1046

How to trigger a jquery plugin after a keyup event from an inline script

In this fiddle you can see the script I am working on that uses this plugin to create estimates. However, this plugin only allows predetermined values in data-attributes to be used in checkbox and radiobutton inputs. However, I need to be able to enter values for what I want to use it for, so I created a field that uses keyup to change the data-attribute named data-cost. This method does update the data-attributes, but does not trigger the script to sum the new values entered.

I wanted to know if there was a edit the plugin or trigger it outside the script when they keyup event happens.

This is the keyup script

$(function () {
    $('#account_balance1').on('keyup blur paste', function() {
        var self = this;
        setTimeout(function() {
            var str = $(self).val();
            $("input[data-cost][debt]").attr('data-cost',str.replace(/^\$/, ''));
            $("input[data-cost][debt]").attr('debt',str.replace(/^\$/, ''));
            calculateTotalFor() // this is how I do it on inline scripts
        }, 0)
    })     
});

This is the input that has the data-attribute changed from the keyup event.

<input type="checkbox" checked="checked" data-cost="100" debt="" value="" name="f_2[]"/>

This is the input where the value is entered for the keyup event

<input type="text" maxlength="7" class="balance" id="account_balance1" name="cardbalance" value=""/>

Upvotes: 0

Views: 240

Answers (1)

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

By checking the plugin source, this solves your problem:

$("input[data-cost][debt]").data('cost',str.replace(/^\$/, ''));
$("input[data-cost][debt]").data('debt',str.replace(/^\$/, ''));
$('#jquery-order-form').data('jprice').onChange();

The problem is that you updated the html attributes, but not the inner data.

Here you have it working: http://jsfiddle.net/edgarinvillegas/8jdfJ/3/

Cheers, from La Paz, Bolivia

Upvotes: 1

Related Questions