javascript - jQuery assign click event handler on page load -


i want assign click handler button in page load function. want handle actual click of button @ later time following code snippet:

 $("#finish").off('click').on('click', function () {             var sku = $("#productsku").val();             var name = $("#productname").val();             var affiliateid = $("#affiliateid").val();             var errors = "";             var category = null;             var defaultname = $('#defaultimgname').val();              if ($("#childcategories_" + categorylevel).length == 0) {                 category = $("#categorylist").val();             }             else if ($("#childcategories_" + categorylevel).val() == 0) {                 category = null;             }             else {                 category = $("#childcategories_" + categorylevel).val();             }              if (!sku) {                 errors += "<li> can not add product without item code</li>";             }             if (!name) {                 errors += "<li> can not add product without name</li>";             }             if (!category) {                 errors += "<li> can not add product without category</li>";             }              if (errors != "") {                 cua({ text: errors, type: 'danger' })             }             else {                 data.formdata = { productname: name, productsku: sku, affiliateid: affiliateid, categoryid: category, defaultimgname: defaultname }                 data.submit();             }         }); 

how assign click event handler in page load, , trigger actual event of click later on?

reason it's causing errors button unresponsive if image not uploaded, found caused event handling problem.

$(document).ready(function(){     $("#finish").on('click', yourfunctionhere) }); 

you can put code in seperate function, if want turn off, on again can 1 line of code.

$("#finish").on('click', yourfunctionhere) 

to execute function can trigger click (when handler on):

$("#finish").trigger('click'); 

(thanks @putvande reminding)


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -