How to solve this problem

Asked

Viewed 100 times

0

I need to make this code work the way that if it contains the word in the url I switch to the default option as follows:

$('#servico').val(Sispostag.getParameterValue('servico'));  
var url = location.href;
if(url.indexOf('undefined')==-1) {                
    $('#servico').val(Sispostag.getParameterValue('servico'));
    var elementS = document.getElementById('servico');
    elementS.dispatchEvent(new Event('change'));
} else {
    $('#servico').val('123');
    var elementS = '123';
    elementS.dispatchEvent(new Event('change'));              
}

But always returns this error: Uncaught Typeerror: Elements.dispatchEvent is not a Function.

Can anyone help me resolve this error?

1 answer

0


If you’re using jQuery, use .change() to trigger the event.

The error in question comes from this line:

var elementS = '123';

and it turns out you’re overwriting the value of the variable, not the element. Then when you call dispatchEvent is the same as

123.dispatchEvent(new Event('change'))

and this gives error.

Code suggestion:

var url = location.href;
if(url.indexOf('undefined')==-1) {                
    $('#servico').val(Sispostag.getParameterValue('servico')).change();
} else {
    $('#servico').val('123').change();           
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.