user4349555 February 2016

jQuery pass clicked image src to fancybox popup form

I have a popup form which is activated upon clicking an image, I've used fancybox to have the lightbox effect. What I would like is for one of the form fields to be auto-filled with the clicked images src. I have tried the following but to no effect (nothing in console shopwing an error):

jQuery("#storefront li img").click(function() {
    var barimg = jQuery(this).prop("src");
    jQuery("input[name=car]").val(barimg);
});

HTML for form:

               <div style="display:none">
                    <form method="POST" action="thanks.asp" id="modalbar">
  <div class="formwrapper">
    <label for="name">Name <span class="red2">*</span></label>
    <input type="text" name="FirstName">
    </div>

  <div class="formwrapper">
    <label for="car">Car (Please include <strong>Make</strong> / <strong>Model</strong> / <strong>Year</strong>) <span class="red2">*</span></label>
    <input type="text" name="Car">
    </div>

    <div class="formwrapper">
    <label for="reg">Reg Number</label>
    <input type="text" name="Reg">
    </div>

  <div class="formwrapper">
    <label for="phone">Telephone Number</label>
    <input type="text" name="Tel">

    </div>

  <div class="formwrapper">
    <label for="email">Email Address <span class="red2">*</span></label>
    <input type="text" name="EmailFrom">

    </div>

  <div class="formwrapper">
    <label for="prefcontact">Preferred Contact Method <span class="red2">*</span></label>
    <input type="text" name="ConMeth">

    </div>

  <div class="formwrapper">
    <label for="maillist">Join Mailing List</label>
    <input type="checkbox" name="MailList" value="Yes" checked="checked">        

Answers


wilsonlee February 2016

Try this~~ I think you missed '' in "";

jQuery("#storefront li img").click(function() {
    var barimg = jQuery(this).prop("src");
    jQuery("input[name='car']").val(barimg);
});

Or jQuery('input[name="car"]') will work too, but jQuery("input[name=car]") will not select any element,I think...

Post Status

Asked in February 2016
Viewed 1,565 times
Voted 4
Answered 1 times

Search




Leave an answer