0

I am returning a JSON from my validation CFC, which contains an array of fields that were found to be in error.

{"error":1,"fields":["first_name","email","last_name","comments"]}

I am using the jquery post method, but when I get the data returned, I cannot create the labels based on the fields in the json.

$.each(data.fields, function(){
    $("#" + this + "\"").append("<label class='error' for='" + this + "'>Please check your input</label>")

But this doesn't appear to work. What am I doing wrong?

Also, I am using jquery validation plugin, but I am required to use server side validation as well. I have disabled the validator.

3 Answers 3

1

You're adding a backslash (\) to your selector which will make your selector fail. Remove the backslash (i.e., $("#" + this)) and it should work.

On another note, the jQuery Validation plugin uses the name attribute for validation, not the id attribute, so unless your server-side validation is returning the id attribute you're going to want to use a selector that targets the name attribute instead.

var selector = 'input[name="' + this + '"], textarea[name="' + this + '"]',
    field = $(selector);
field.after($('<label />').attr('for', field.attr('id')).addClass('error').text('Please check your input'));

Or use this to target the id attribute:

var selector = '#' + this,
    field = $(selector);
$(selector).after($('<label />').attr('for', field.attr('id')).addClass('error').text('Please check your input'));

Here's a fiddle showing it in action: http://jsfiddle.net/aHLgw/

Finally, while there's technically nothing wrong with using the this keyword, it would be a better practice to use the key and value parameters as it will more clearly indicate to others who may have to maintain the code you write that you are iterating through an object or an array, instead of a DOM Element Collection.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works exactly as intended. I have always struggled with variables as jQuery selectors. I have also changed it to use the name attribute, because the name and id are the same, and I have changed it to use the key/value pairing as well. Thank you.
1

Two things :

  • You need to pass two parameters to each. this will return a string of arrays. So, consider I'm logging this and the parameters passed to each.

    $.each(data.fields, function (key, value) {
      console.log("when this is used - a string array: ",this);
      console.log("value (parameter passed to each) is used - string : ", value);
    });
    

    Here's the console output for the following code (when "comments" string is in scope)

    Console output More info at docs : http://api.jquery.com/each/

  • $("#" + value + "\"") won't work here. Just $("#" + value) would do.

    $.each(data.fields, function (key, value) {
       $("#" + value).append("<label class='error' for='" + value + "'>Please check your input</label>")
    });
    

Hope this helps!

Comments

-1

Use this.text(), that will return the actual value of the elements or use a callback function as told by hungerpain above.

Comments

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.