Tagged: Form select element selected
Hello,
I am lookin for a way to set a specific option in the Form Select Element as “selected”. Can someone help with a tip on how to do that?
Hey lyubopopov,
Thank you for the link to your site, try adding this code to the end of your child theme functions.php file in Appearance ▸ Editor, or if you are not using a child theme you could use the WP Code plugin then add a new snippet, in the top right corner use the PHP snippet as the code type:
then add this code and save:
function select_value_in_form() { ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
var selectFields = document.querySelectorAll('.page-id-15 #avia_6_1');
selectFields.forEach(function(selectField) {
for (var i = 0; i < selectField.options.length; i++) {
if (selectField.options[i].value === 'Brazil') {
selectField.selectedIndex = i;
break;
}
}
});
});
</script>
<?php
}
add_action( 'wp_footer', 'select_value_in_form', 99 );
Please ensure to copy the code from the forum and not an email notification so the symbols are not converted.
I wrote this to only work on the one page so it won’t interfere with other pages.
Best regards,
Mike