Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery click function inside form submit function

Tags:

jquery

php

I have a form with number of submit type as images. Each image has a different title. I need to find out the title of the clicked image. But my click function inside form submit is not working.

My form is:

  <form action='log.php' id='logForm' method='post' >
  <?
   for($j=1;$j<=5;$j++)
   {
    ?>
   <input type="image" src="<?=$img;?>" title="<?=$url;?> id="<?="image".$j?> class="images" />
    <?
    }
    ?>
   </form>

Jquery:

    $("#logForm").submit(function(e)
    {


  $(".advt_image").click(function(event) {
        var href=event.target.title;
    });





           var Form = { };
            Form['inputFree'] = $("#inputFree").val();
         //   if($("#freeTOS").is(":checked"))
                    Form['freeTOS'] = '1';

            $(".active").hide().removeClass('active');
            $("#paneLoading").show().addClass('active');


var url="http://"+href;

      $.post('processFree.php', Form, function(data)
            {
                    if(data == "Success")
                    {
                            $("#FreeErrors").html('').hide();
                            swapToPane('paneSuccess');

                     setTimeout( function() {  location=url }, 2500 );


                    return;




                    }

                    swapToPane('paneFree');
                    $("#FreeErrors").html(data).show();
            });

            return false;
    });

How can I get the title value of clicked image inside this $("#logForm").submit(function())?

How can I use the id of clicked image for that?

like image 221
Zendie Avatar asked May 19 '26 17:05

Zendie


1 Answers

You can use event.target property

$("#logForm").submit(function(e)
    alert($(e.target).attr('title'));
});

http://api.jquery.com/event.target/

[UPDATE]

I just realized it wouldn't work. I don't think there is a simple solution to this. You have to track the click event on the input and use it later.

jQuery submit, how can I know what submit button was pressed?

$(document).ready(function() {
    var target = null;
    $('#form :input[type="image"]').click(function() {
        target = this;
        alert(target);
    });
    $('#form').submit(function() {
        alert($(target).attr('title'));
    });
});

[Update 2] - .focus is not working, but .click is working http://jsfiddle.net/gjSJh/1/

like image 106
Joyce Babu Avatar answered May 22 '26 06:05

Joyce Babu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!