3

I have a Bootstrap modal that shows the images of Gallery, when you click on it. I want change input value by clicking on each image and put their Url on input text for submit . But it doesn't work and value of input text doesn't change . Why?

    @model IEnumerable<tourism.Models.Gallery>

    <input type="text" name="imgUrl" id="index-img" style="width: 100%;" value="url" />

 <div class="container">
    <!-- Trigger the modal with a button -->
    <button type="button" class="btn btn-info btn-lg" id="myBtn">Choose Image</button>

    <!-- Modal -->
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog modal-lg">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Gallery</h4>
                </div>
                <div class="modal-body">

                    <div class="row">

                        @foreach (var item in Model)
                        {
                            <div class="col-lg-2 col-sm-3 col-xs-4">
                                <a href="#" onclick="(function () {    ('input[name=imgUrl]').val()[email protected](modelItem => item.Url); })()" class="close" data-dismiss="modal"><img src="@Html.DisplayFor(modelItem => item.Url)" class="thumbnail img-responsive" alt="@Html.DisplayFor(modelItem => item.Alt)"></a>
                            </div>
                        }

                    </div>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>

        </div>
    </div>

</div>

jQuery code for passing Url from model to input text(from above code) :

<a href="#" onclick="(function () {    ('input[name=imgUrl]').val()[email protected](modelItem => item.Url); })()" class="close" data-dismiss="modal"><img src="@Html.DisplayFor(modelItem => item.Url)" class="thumbnail img-responsive" alt="@Html.DisplayFor(modelItem => item.Alt)"></a>

1 Answer 1

3

Change the link to add a data- value for the items url (remove your onclick=...)

<a href="#" data-url="@item.Url" class="image" .... >

and then add the script to handle the click event of the link to update the input

$('.image').click(function() {
  $('#index-img').val($(this).data('url'));
});
Sign up to request clarification or add additional context in comments.

5 Comments

Well done . Thank you sir . do you have some good source for learning jquery?
sorry , but what is role of 'data-url' here?
I would first start with understanding Unobtrusive_JavaScript. The best site IMO is the official documentation
Html data- attributes are a way of adding arbitrary data to html elements (refer the jquery documentation here). In this case, its storing the value of your objects Url property to a data property named url which you can then retrieve using .data('url'). You can store pretty much anything you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.