Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:35:24+00:00 2026-06-15T02:35:24+00:00

I have the following HTML: <input class=button name=save onclick=$(this).replaceWith(‘<img src=http://www.example.com/images/ajax-loader.gif />’); type=submit value=SUBMIT> When

  • 0

I have the following HTML:

<input class="button" name="save" onclick="$(this).replaceWith('<img src=http://www.example.com/images/ajax-loader.gif />');" type="submit" value="SUBMIT">

When clicking on the button, the form no longer attempts to submit, but replaces itself with a turning AJAX loading gif and does nothing. If I remove the onclick portion, it submits.

Long story, this is for a client, but I can’t give them the same thing in the form of

$(function() { $(input[name=save]).on({click:function() { $(this).replaceWith(...); } }); });

Why won’t this still submit after replacing itself? I’m not e.preventDefault()‘ing anywhere.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:35:27+00:00Added an answer on June 15, 2026 at 2:35 am

    According to the replaceWith() documentation:

    The .replaceWith() method removes content from the DOM and inserts new
    content in its place with a single call.

    When an element is removed, as per remove() documentation:

    In addition to the elements themselves, all bound events and jQuery
    data associated with the elements are removed.

    I’m assuming that is why the submit is not executing.

    If you want the form to still submit you can manually trigger the submit after you have replaced the element.

    DEMO – Submitting the form while replacing the button element

    For readability, to demonstrate, I have moved the binding of the click event into script rather than in-line of the element. I also added an id in case of multiple forms, which is not really needed if you only got a single form as you can simply bind to $("form").on("submit") instead then

    $("input[name='save']").on("click", function(){
        $(this).replaceWith('<img class="submit-form" src=http://www.example.com/images/ajax-loader.gif />');
        $("form").submit();
    });
    
    $("#myForm").on("submit", function(){
        alert("form has been submitted.");
        return false;
    });
    

    ​Edit – Same code using in-line onclick

    Moving the code into the in-line onClick still works.

    Regarding the form submission, $(this) will be the button before it is replaced and be gone after, hence $(this).closest("form").submit() or any other form of selector using $(this) won’t work.

    You must target your form for submit neutrally without using $(this) by either using $("form").submit() or if you have mutiple forms use an id as in $("#myForm").submit().

    <form id="myForm" action="http://jsfiddle.net/">
        <input class="button" name="save" type="submit" onclick="$(this).replaceWith('<img src=http://www.example.com/images/ajax-loader.gif />'); console.log(this); $('#myForm').submit();" value="SUBMIT">
    </form>
    

    DEMO – Submitting a form manually after replacing the button using inline onclick

    As a side note, if you want the image to be displayed before the submit event is triggered you can apply a little trick like this:

    $("input[name='save']").on("click", function(){
        $(this).replaceWith('<img class="submit-form" src=http://www.example.com/images/ajax-loader.gif />');
    
        // Makes sure the image is rendered before the submit executes.
        window.setTimeout(function(){
            $("form").submit();
        }, 300);
    });
    

    You can apply the same off course in your in-line onClick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML: Radio button list(s): <input name=rdLst163 class=required_Mandatory_RbtLst id=rdLst163_1 onclick=fn(1631); type=radio
I have the following HTML: <label class=fileinput-button> <span>add file...</span> <input type=file name=file disabled> </label>
I have the following HTMl: <input id=brandID name=brandID type=hidden value= /> <select id=selectList class=textfield70pc
I have the following HTML: <div class=row1> <input type=radio name=group1 value=0 checked=checked/>Zero <input type=radio
I have the following html <tr class=editorRow> <td class=emailRow> <input id=EmailSuccessList_0 name=EmailSuccessList[0] type=text value=asdf@lib.com>
I have the following HTML: <div class=result-row odd> <div class=domain-name>first-domain.com</div> <div class=domain-functions> <a onclick=deleteDomain(1)
I have a form. With the following: HTML: <form name=feedback_form method=post action= class=feedback_form> <input
I have the following HTML: <tr> <td> <input type=checkbox class=showFilter /> Ranks </td> </tr>
I have the following html code: <form> <table> <tbody> <tr> <td> <span>Quantity<span class=pull-right>3</span> <input
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.