Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript I need help with select options javascript code.

HBNA

New Coder
Hi I have this code in the head section of my page:


HTML:
<script src="https://cdn.jsdelivr.net/npm/@feathery/react@latest/umd/index.js"></script>
<script>
    (() => {
        Feathery.init('mykey'); // Replace 'mykey' with your actual Feathery SDK key

        const formMap = {
            'form1': 'PB Home Purchase', // Replace 'Form 1 Name' with the actual name of the form
            'form2': 'PB Refinance', // Replace 'Form 2 Name' with the actual name of the form
            'form3': 'PB Contact', // Replace 'Form 3 Name' with the actual name of the form
            'form4': 'PB Contact' // Replace 'Form 4 Name' with the actual name of the form
            // Additional form names can be mapped here
        };

        function handleSelectChange() {
            const formType = document.getElementById('formSelector').value;
            if (formType) {
                openPopup(formType);
            }
        }

        function openPopup(formType) {
            const popup = document.getElementById('popup');
            const existingContainer = document.getElementById('container');

            // Remove the existing container if it exists
            if (existingContainer) {
                existingContainer.parentNode.removeChild(existingContainer);
            }

            // Create a new container and append it to the popup
            const newContainer = document.createElement('div');
            newContainer.id = 'container';
            newContainer.style.position = 'absolute';
            newContainer.style.top = '50%';
            newContainer.style.left = '50%';
            newContainer.style.transform = 'translate(-50%, -50%)';
            popup.appendChild(newContainer);

            // Render the form in the new container
            Feathery.renderAt('container', { formName: formMap[formType] });
            popup.style.display = 'block';
        }

        function closePopup() {
            document.getElementById('popup').style.display = 'none';
        }

        window.handleSelectChange = handleSelectChange;
        window.closePopup = closePopup;
    })();
</script>

Then in the body of my page I have this code:

HTML:
<div id='popup' style='display:none; position:fixed; top:0; left:0; right:0; bottom:0; background:rgba(0,0,0,0.5);' onclick="closePopup()">
    <div id='container' style='position:absolute; top:50%; left:50%; transform:translate(-50%, -50%);background-color: white;' onclick="event.stopPropagation()">
        <!-- Dynamic form will be rendered here -->
    </div>
</div>

<!-- Dropdown for selecting form type -->
<select id="formSelector" onchange="handleSelectChange()">
    <option value="">CHOOSE AN OPTION</option>
    <option value="" style="font-size:3pt;" disabled>&nbsp;</option>
    <option value="form1" style="height:20px;">Option 1</option>
    <option value="" style="font-size:3pt;" disabled>&nbsp;</option>
    <option value="form2">Option 2</option>
    <option value="" style="font-size:3pt;" disabled>&nbsp;</option>
    <option value="form3">Option 3</option>
    <option value="" style="font-size:3pt;" disabled>&nbsp;</option>
    <option value="form4">Option 4</option>
    <!-- Additional options can be added here -->
</select>

I have a few issues I just can't figure out.

1. The first form (Option 1) works just fine but when I open the other forms (Options 2-4) and then click in on the forms, the forms just close. I can't move on through the form. Every time I try, they just close.

2. When I click on an Option to open a form, the form opens up, then I click off the form, it closes as it should. But then when I click on the same option to open the same form I just previously opened, the form does not open. The only way to open up the same form I just closed is to select another Option, open that form and then close it (or click off of it) and then go back to the previous Option (form) I opened up.

Please help.

Thank you
 
Last edited by a moderator:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom