troglodyte07
troglodyte07

Reputation: 4066

Opening a modal window with onclick of a button with preset form field value

I have a button called view. On clicking the view button, a modal window should pop up with a given value

Below is the code for for view button which pops a modal with empty body.

<a role="button" class="btn btn-sm btn-primary" href="#time_line" data-toggle="modal">View</a>

Modal div:

<div id="time_line" class="modal fade" tabindex="-1">
        <div class="modal-header" style="background: #045e9f;">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="white bigger">Initiate Order</h4>
        </div>

        <div class="modal-body overflow-visible">
            <form>
                <input type="text" name="t_val" id="t_val">
            </form>
        </div>

        <div class="modal-footer">
            <button class="btn btn-small" data-dismiss="modal">
                <i class="icon-remove"></i>
                Close
            </button>
        </div>  
</div>

How do I pass a value to the val field in the modal upon the modal pop

Answer:

Hi, I did the following and got what I want

function pass_data(data)
{
    $('[name="t_val"]').val(data);
}

<a role="button" class="btn btn-sm btn-primary" href="#time_line" data-toggle="modal" onclick= "pass_data($data)">View</a>

A slight change to the answer by DarkseidNG

Upvotes: 0

Views: 3422

Answers (1)

DarkseidNG
DarkseidNG

Reputation: 92

create a javascript function and pass the value from the onClick to the function then call your modal in the function

<a role="button" class="btn btn-sm btn-primary" href="#time_line" onclick = "doFunction('<?php echo $xyz; ?>')">View</a>

<script>
   function doFunction(data){
            //if you have a html tag in the modal that you want to pass the value to you can do this
           $(".data-tag").html(data);
            $("#time_line").modal();
    }

Upvotes: 1

Related Questions