Sakib
Sakib

Reputation: 126

Jquery auto submit not working

I have a problem. I want when I click an h1 tag, then h1 tag id add in input value and Submitted automatically. But auto submits is not working. If I click submit button then its submitted data.

   <h1 id="my-id">sadasdsa</h1>
<form action="" method="POST" id="aweberform">
    <p><input type="text"  name="countrycode"  value="" id="country"></p>
    <button type="submit"  name="submit"  class="btn">Submit</button>
</form>


Jquery:

$(document).on('click', 'h1', function () {
    //alert(this.id);
    $("h1").text(this.id);
    $("input").val(this.id);
    $("#aweberform").submit();       
});
$(document).click(function(){
    $("#aweberform").submit();
});

Upvotes: 2

Views: 1209

Answers (4)

Shakti Phartiyal
Shakti Phartiyal

Reputation: 6254

You can use a simpler approach as your button type is submit. Allot an id to it and on document click programmatically click on it.

<button type="submit"  name="submit"  class="btn" id="submitBTN">Submit</button>

THE JS:

$(document).click(function(){
    $("#submitBTN").click();
});

Whatever be the event if you want to submit the form, you can always follow the above mentioned approach.

Upvotes: 1

Abi
Abi

Reputation: 734

In Your JQuery You mention You mention path but what is that

just simply change that into id of h1 tag So changes are as follows

$(document).on('click', '#my-id', function () {

Running Example

$(document).on('click', '#my-id', function () {
    //alert(this.id);
    $("h1").text(this.id);
    $("input").val(this.id);
    $("#aweberform").submit();       
});
$(document).click(function(){
    $("#aweberform").submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="my-id">sadasdsa</h1>
<form action="" method="POST" id="aweberform">
    <p><input type="text"  name="countrycode"  value="" id="country"></p>
    <button type="submit"  name="submit"  class="btn">Submit</button>
</form>

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26298

The problem is here:

$(document).on('click', 'path', function () {

what is path here?

you have to use either id, class, name of the attribute as a selector, but in your case there is nothing with path in html. So change path to:

$(document).on('click', '#my-id', function () {

and try again.

Upvotes: 1

Super User
Super User

Reputation: 9642

Just update #my-id instead of path:

$(document).on('click', '#my-id', function () {
    //alert(this.id);
    $("h1").text(this.id);
    $("input").val(this.id);
    $("#aweberform").submit();       
});
$(document).click(function(){
    $("#aweberform").submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="my-id">sadasdsa</h1>
<form action="" method="POST" id="aweberform">
    <p><input type="text"  name="countrycode"  value="" id="country"></p>
    <button type="submit"  name="submit"  class="btn">Submit</button>
</form>

Upvotes: 0

Related Questions