1193
Views
3
Comments
Solved
Addeventlistener
Question

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

2020-02-28 09-46-54
Eduardo Jauch
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

2012-03-02 13-17-21
Paulo Ramos
Staff

Hi,

Check out jQuery's documentation for click(): https://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.

2020-02-28 09-46-54
Eduardo Jauch
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

2024-10-25 09-14-42
Christopher Bautista

Hi Paulo/Ed,


Thanks Guys.

Christopher

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.