Addeventlistener

Addeventlistener

  

Good Day Everyone!


I'm trying to add in the Javascript editor of a web block the below script:


var comboboxetypeprimary = $('.etypeprimary').attr('id');

document.getElementById(comboboxetypeprimary).addEventListener("change", myFunction);

function myFunction() {
    var buttonvalidateid = $('.buttonvalidate').attr('id');
    var buttonvalidateclass = document.getElementById(buttonvalidateid);
       
    if (typeof buttonvalidateclass.onclick == 'function')
    {                
        buttonvalidateclass.onclick.apply(buttonvalidateclass);
    }
}


My primary objective is to validate all combo box and input widgets if changes were made.

I'm currently testing it for a single combo box.

I can't seem to make it work.

Need your kind assistance


Thank you and regards,

Christopher

Hi,

Check out jQuery's documentation for click(): http://api.jquery.com/click/

You should be able to do it with this:

$("#wtButton1").click(myFunction);

Button1 is the name property of the button (use a class selector instead if you wish) and myFunction is your handler function (note: if this function returns false, the button won't submit to the server).

You may need to run the code above using HttpRequestHandler's RunJavascript action in the block's Preparation, to ensure the DOM is already loaded.

Solution

Hello Christopher,

Why don't you use the OnChange Action instead? 

Using JavaScript to do validation client side will make your code a mess and much more error prone.

If you are concerned with performance (You should, of course), why not validate everything on submit, as this is the traditional approach?

Your code will be much more clear, performance will be the same, and is the usual behaviour (validate on submit). For some critical inputs you can use the OnChange.

Cheers

Solution

Hi Paulo/Ed,


Thanks Guys.

Christopher