How to solve this problem [closed]

0

I need to make this code work so that if I contain 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 it always returns this error: Uncaught TypeError: elementS.dispatchEvent is not a function.

Can anyone help me solve this error?

    
asked by anonymous 03.05.2017 / 14:25

1 answer

0

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

The error in question comes from this line:

var elementS = '123';

And it happens that you are overwriting the value of the variable, not the element. Then when you call dispatchEvent it is the same as

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

and this gives error.

Code hint:

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