-2

My code is like this

<div class="control-group">
                <form>
                    <label for="stu-name">Search a Student</label>
                    <select id="stu-name" required class="demo-default" placeholder="Who are you looking for..">
                        <option value="">Who are you looking for..</option>
                        <option value="4" id="4">Thomas Edison</option>
                        <option value="1" id="1">Nikola</option>
                        <option value="3" id="3">Nikola Tesla</option>
                        <option value="5" id="5">Arnold Schwarzenegger</option>
                    </select>
                    <div style="margin-top:20px">
                        <button type="submit" href="#" onclick="MyFunction();return false;">Submit</button>
                        <script type="text/javascript">

                        function myFunction() 
                        {
                        var element = document.getElementById(id);
                        }

                        </script>
                    </div>
                </form>
            </div>

I have 15 HTML files named 1.html 2.html 3.html etc. I need to open these with their id's. Is it possible to do so?

For example, if I select Nikola then 1.html should be opened in a new window.

Zombo
  • 1
  • 62
  • 391
  • 407

1 Answers1

0

If you want to open a new browser window targeting a file based on the ID of the selected option in your <select>, then try this:

jQuery(function($) {
    $('form').on('submit', function(e) {
        e.preventDefault();
        var id = $(this).find('#stu-name option:selected').attr('id');

        window.open(id + '.html');
    });
});

For this given HTML:

<form>
    <label for="stu-name">Search a Student</label>
    <select id="stu-name" required class="demo-default" placeholder="Who are you looking for..">
        <option value="">Who are you looking for..</option>
        <option value="4" id="4">Thomas Edison</option>
        <option value="1" id="1">Nikola</option>
        <option value="3" id="3">Nikola Tesla</option>
        <option value="5" id="5">Arnold Schwarzenegger</option>
    </select>
    <div style="margin-top:20px">
        <button type="submit">Submit</button> <!-- no need for the 'onclick' attribute now and <button> doesn't have a 'href' attribute, <a> does -->
    </div>
</form>
D4V1D
  • 5,805
  • 3
  • 30
  • 65