Note that there are some explanatory texts on larger screens.

plurals
  1. POScript loaded through .load() fails
    primarykey
    data
    text
    <p>Im loading a contact page using <code>.load()</code> which contains a script that looks like this: </p> <pre><code>&lt;script type="text/javascript"&gt; $('#submit').click(function(e){ e.preventDefault(); var name = $('#name').val(); var email = $('#email').val(); var message = $('#message').val(); $.post("&lt;?php echo bloginfo( 'template_directory' ) . '/inc/mailit.php';?&gt;", { name: name, email: email, message: message }, function(data) { var n = data.indexOf('&lt;span class="success_message"&gt;'); if (n !== 0) { $('#message_box_1').empty().append(data); } else { $('#contact_form').empty().append(data); } } ); }); &lt;/script&gt; </code></pre> <p>The script works fine when I load the contact page directly (i.e. go to <code>http://example.com/contact-us</code>) but it doesn't when I load the form and script into the dom using <code>.load()</code>. When I click <code>#submit</code>, the form submits without running the script. Any ideas what is going on?</p> <p>Oh and the load script looks like this: </p> <pre><code>&lt;script&gt; $('#top_links a').click(function (e) { e.preventDefault(); var link = $(this).attr('href'); $('#content').empty(); $('#content').load(link + ' #content &gt; *'); }); &lt;/script&gt; </code></pre> <p><strong>UPDATE:</strong></p> <p>Thanks to @pointy for pointing out the problems with <code>.load()</code>. So now I have modified my code to use <code>$.get</code> :</p> <pre><code>&lt;script&gt; $('#top_links a').click(function (e) { e.preventDefault(); var link = $(this).attr('href'); $('#content').empty(); $.get(link, { }, function(data) { $('#content').replaceWith($(data).find("#content")); } ) }); &lt;/script&gt; </code></pre> <p>The problem is the same, the AJAX Script above still doesn't prevent the form from submitting or send a post request when the user clicks the submit button. I have placed both scripts outside of the content that is being loaded with <code>$.get</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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