javascript - Jquery executing/triggering same code on different events -


i have function onclick event of custom tag densitybtn='yes'

$("[densitybtn='yes']").on('click', function () {     var no_of_people = 0;     //calcdensity     $("[calcdensity='yes']").each(function () {         no_of_people = parseint($(this).val()) + no_of_people;     });     var total_density = parseint(carpetarea) / parseint(no_of_people);     $("#densityval").html(myval); }); 

can extend same code extending $("[calcdensity='yes']").on('blur')

$("[calcdensity='yes']").on('blur').$("[densitybtn='yes']").on('click', function () {  }); 

am not sure on executing same code on different events

let me know method correct? or there alternative way available?

define function (not anonymous function) , pass function event listeners

function listener() {    var no_of_people = 0;    //calcdensity    $("[calcdensity='yes']").each( function() {       no_of_people = parseint($(this).val())+no_of_people;    });    var total_density = parseint(carpetarea)/parseint(no_of_people);    $("#densityval").html(myval); }  $("[densitybtn='yes']").on('click', listener); $("[calcdensity='yes']").on('blur', listener); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -