Select Default Dropdown on Page load Javascript

I want to set a default value of a dropdown every time a page loads.

Looking to add in a javascript on the page load.

Here is the page:

https://gearheart.knack.com/gearheart-broadband-structure-1#enter/zip/5b22126b7f67c03d97807f8c/

I want to change the top drop down to default to Residential on each page load.

Right now, it saves whatever was set last.

The select ID is view_113-field_460 and the value of the Residential option is 5b493d248b95865cd138ef0e

 

How would I set the option to Residential on each page load by default?

 

Thanks

 

Does anyone know if this is still working?  I used to use a similar method, but it stopped a while back and I haven't been able to make it work again.

 

I think you forgot to changes field Id .

 

It's working fine and i'm using it on many Projects with same way.

 using 

$("#view_6-field_217").val([]).trigger('liszt:updated'); 

it's also a batter way to reduce 2 lines of code into single line

and then we can trigger change function if required

Thanks

For those interested, this worked...

 

$("#view_6-field_217").val([]).trigger('liszt:updated');

Thanks. Now I'm logging the following error.

Uncaught TypeError: Cannot read property 'length' of undefined

I believe this has something to do with Chosen jQuery plugin. Does anyone know if there is a way to call the first "Select..." option instead of entering a blank value? 

Hi Bharvey,

 

Yes it's simple just use below code to reset it's values

 

document.getElementById('view_113-field_460').value = "";
$("#view_113-field_460").trigger("liszt:updated");
$("#view_113-field_460").chosen().trigger("change");

Please mail me for any help

Thanks

Sunny Singla

ssingla1985@gmail.com

+919855089359

Hey Sunny, and do you know how to reset the drop down to the default "Select..." option? I would like to be able to reset the dropdown value based on a .focusout

 

$(document).on('knack-view-render.view_6', function(event, scene) {
$('#view_6-field_227').focusout(function() {
var driveType = $('#view_6-field_227').val();
if (driveType.includes("Yes")) {
$('#view_6_field_217_chzn').selectedIndex = "0";
$("#view_6-field_217").chosen().trigger("change");
}
});
});

Thanks again Sunny!

I appreciate your help!

Your welcome Brad ..

Thanks

Sunny Singla

 

 

Beautiful Sunny - thanks for sharing!

The other option without code is to set the dropdown field to default to the first option and make sure Residential is on top of the list - we've finagled that by adding spaces to some items where they're sorted alphabetically.

Doesn't always suit though and we use something like Sunny's code to achieve the same outcome where the lists don't change much.

Hi Jason,

 

Try below code

 

$(document).on('knack-view-render.view_113', function (event, view, data) {
document.getElementById('view_113-field_460').value = "5b493d248b95865cd138ef0e";
$("#view_113-field_460").trigger("liszt:updated");
$("#view_113-field_460").chosen().trigger("change");
});

 

Regards,

Sunny Singla

ssingla1985@gmail.com

+919855089359