Search code examples
javascripthtmlhref

get the href value from selected option and change the href of the button


I want to get the href value of the selected option so that i can now go to the link that i selected using the button.

Here's my code

<div class="modal fade" id="soa">
        <div class="modal-dialog">
            <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title">Choose the name of student</h4>
                </div>

                <div class="modal-body">
                    <div class="col-md-12">
                         <div class="form-group">
                            <label class="control-label"><span
                               class="text-danger"></span> Name of Student</label> 
                            <select name="childNames" class="form-control input-lg" id="childNames" data-size="5">
                               <option value="" selected disabled></option>
                               <option value="${pageContext.request.contextPath}/app/billsandpayments/invoice">John Dela Cruz</option>
                               <option value="${pageContext.request.contextPath}/app/billsandpayments/invoice1">Josh Dela Cruz </option>
                               <option value="${pageContext.request.contextPath}/app/billsandpayments/invoice2">Jane Dela Cruz</option>
                            </select>
                         </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <a href=''><button type="button" class="btn btn-info">View</button></a>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

Solution

  • Add a id to =>

    <a id="link" href=''><button type="button" class="btn btn-info">View</button></a>
    

    This should work :

    $(document).ready(function() {
    
        $("#childNames").on('change', function () {
    
                    var ref = document.getElementById("childNames").value;
                    alert(ref);
                    $('#link').attr('href', ref);
    
        });
    
    });
    

    jsfiddle example