Situation and realisations
I'm implementing a WP plugin and have to insert a new post in the wp database. So I created a form that display when the button buttonCreateInv
is clicked. I use jQuery and it does work.
<?php
add_action('admin_head', 'JS_create_form_inventaire');
add_action('wp_ajax_display_form_inventaire', 'display_form_inventaire');
function button_create_inventaire(){
echo '<button type="button" class="buttonCreateInv">Créer un inventaire</button>';
echo '<div class="formCreateInv"></div>';
}
function JS_create_form_inventaire(){
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('.buttonCreateInv').click(function(){
$.get(
ajaxurl,
{ 'action': 'display_form_inventaire' },
function (result) {
$(".formCreateInv").html(result);
}
);
});
});
</script>
<?php
}
It does display my form in the function display_form_inventaire
:
function display_form_inventaire() {
...
$html .='<p><label for="description">Content</label></p>'.
'<textarea id="textareaContent" name="textareaContent" style="display:none"></textarea>'.
'<div style="background-color:white" id="divContent" name="divContent" contenteditable="true">'.
'<table>...</table></div>';
$html .='<p><input type="submit" value="Créer" tabindex="6" class="submitInsert" name="submitInsert" /></p>'.
'<input type="hidden" name="creation_inventaire" value="new_post" />'.
wp_nonce_field( 'new-post' ).
'</form>'.
'</div>';
echo $html;
}
Problem
The second time I use it, with exactly the same manner, it is not working..
My second jQuery which is activated on the click on the submit button submitInsert
seem to not being called.
<?php
add_action('admin_head', 'JS_get_inv_content');
add_action('wp_ajax_insert_new_inventaire', 'insert_new_inventaire');
function JS_get_inv_content(){
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
$('.submitInsert').click(function(){
alert('test');
$.get(
ajaxurl,
{ 'action': 'insert_new_inventaire' },
function (result) {
alert('result');
//$.getElementById("textareaContent").value = $.getElementById("divContent").innerHTML;
}
);
});
});
</script>
<?php
}
The alert test is not displayed when I click on the submitInsert
button.