0

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!

4
  • 3
    you dont need to add . (dot) on your html class Commented May 7, 2015 at 12:10
  • Note id's need to be unique, you don't want to have two (type)'s. Commented May 7, 2015 at 12:13
  • Your code works fine for me(I tested till alert() ofcrse) after your edit. here Commented May 7, 2015 at 12:17
  • Your click event shud listen on value-field class and not field-value. Commented May 7, 2015 at 12:29

3 Answers 3

3

The . is for Query selector to know that it's getting a class, you don't want to use it when defining one. So remove the . in class=".field-value"

<td ... class="field-value">value</td>
Sign up to request clarification or add additional context in comments.

2 Comments

The dot was a mistake i made when i recreated the structure, it's not there in the actual document. I've edited my question to reflect this.
@ooglyeyes Seems to work fine then. Make sure you click the "value" text and ensure jQuery was added properly.
0

remove the dot from your class name:

it should be:

<td id="(type)" class="field-value">value</td>  

Instead of:

<td id="(type)" class=".field-value">value</td>

1 Comment

The dot was a mistake i made when i recreated the structure, it's not there in the actual document. I've edited my question to reflect this.
-2

Change:

jQuery(document).on("click", ".field-value", function (e) {

to

jQuery(".field-value").on("click", function (e) {

In your initial version you are basically looking for a document element in the descendants of .field-value.

1 Comment

It's the other way around. Both variants do the same except that the OP's example also extends to dynamically added .field-value elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.