Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create a button with a function, then submit it, with jQuery?
    text
    copied!<p>I am using the following code to add a button to a page</p> <pre><code>$("#myDiv").html("&lt;button id='fileUpload'&gt;Upload&lt;/button&gt;"); </code></pre> <p>I am then creating an <a href="http://valums.com/ajax-upload/" rel="nofollow noreferrer">Ajax Upload</a> instance on the button.</p> <pre><code>var button = $('#fileUpload'), interval; new AjaxUpload(button, { action: '/upload.ashx', name: 'myfile', onSubmit: function(file, ext) { button.text('Uploading'); this.disable(); // Uploding -&gt; Uploading. -&gt; Uploading... interval = window.setInterval(function() { var text = button.text(); if (text.length &lt; 13) { button.text(text + '.'); } else { button.text('Uploading'); } }, 200); }, onComplete: function(file, response) { button.text('Upload'); window.clearInterval(interval); } }); </code></pre> <p>What I want to do is append the button to the page then simulate clicking it automatically. How would I go about doing this?</p> <p><strong>Update</strong></p> <p>The code now reads:</p> <pre><code>$("#myDiv").html("&lt;button id='fileUpload'&gt;Upload&lt;/button&gt;"); var button = $('#fileUpload'), interval; new AjaxUpload(button, { action: '/upload.ashx', name: 'myfile', onSubmit: function(file, ext) { button.text('Uploading'); this.disable(); // Uploding -&gt; Uploading. -&gt; Uploading... interval = window.setInterval(function() { var text = button.text(); if (text.length &lt; 13) { button.text(text + '.'); } else { button.text('Uploading'); } }, 200); }, onComplete: function(file, response) { button.text('Upload'); window.clearInterval(interval); } }); $('#fileUpload').click(); </code></pre> <p>The .click event does not seem to fire. It is reached in the code but does nothing... </p> <p>** Update **</p> <pre><code>$('#fileUpload').click(); </code></pre> <p>needs to be </p> <pre><code> $('input').click(); </code></pre> <p>Please check the accepted answer for why.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload