I'm currently working on a wordpress plugin for a customer and i've run into some trouble with a jQuery script.
I have a table with this structure:
<table>
<tr>
<td>Element</td>
<td class="field-value" data-type="radio">value</td>
</tr>
<tr>
<td>Element</td>
<td class="field-value" data-type="text">value</td>
</tr>
</table>
And so on. I want to make .field-value editable with jQuery.
Here is my script:
jQuery(document).ready(function () {
jQuery(document).on("click", ".field-value", function (e) {
e.preventDefault();
alert("clicked");
var OriginalContent = jQuery(this).text();
var type = jQuery(this).attr("data-type");
console.log("Clicked");
if (type == "radio") {
var q_id = jQuery(this).attr("id");
var type = jQuery(this).parent().prev(".value-field").attr("id");
var a = jQuery(this);
console.log(this);
jQuery.ajax({
type: "post",
data: {
q_id: q_id,
type: type
},
url: "wp-content/plugins/cg-form-plugin/get-options.php",
dataType: "html",
success: function (data) {
jQuery(a).parent().prev(".value-field").html(data);
},
});
} else if (type == "text") {
var a = jQuery("input[name=edit]").val();
var qu_id = jQuery("input[name=edit]").closest(".value-field").next().find(".edit").attr("id");
var v_id = jQuery(this).closest(".et_pb_toggle").attr("id");
var type = jQuery("input[name=edit]").attr("id");
jQuery(this).html("<input type='text' value='" + OriginalContent + "' />");
jQuery(this).children().first().focus();
jQuery(this).children().first().blur(function (e) {
var newContent = jQuery(this).val();
jQuery.ajax({
type: "post",
data: {a:a, qu_id: qu_id, v_id:v_id, type: type},
url: "wp-content/plugins/cg-form-plugin/update-array.php",
dataType: "html",
success: function(data) {
location.reload();
},
});
});
}
});
});
My problem is, that the .on("click") event doesn't trigger. I've been playing around with it for some hours now, trying .click(); and some other stuff.
I have other functions in the script with similar .on("click") events, which works just fine. I am at a loss as to what's wrong.
Anyone able to point me in the right direction?
Edit: fiddle with actual table: http://jsfiddle.net/qr7477ea/2/
**** me, it's value-field not field-value. 3 hours wasted on something so stupid. Thanks anyway ppl!
id's need to be unique, you don't want to have two(type)'s.alert()ofcrse) after your edit. hereclickevent shud listen onvalue-fieldclass and notfield-value.