Friday 9 December 2011

jQuery and dot notation

I'm still a relative noob when it comes to jQuery, so I'm picking bits up all the time. The latest nugget is related to a previous post on converting an HTML to JSON which included the parsing of nested structures.
When it comes to populating a form with a nested structure there is potential to fall into a rabbit hole. Thankfully in this case the foxes had scared off the rabbits before they had the chance to dig too deeply.
The original issue manifested itself upon requesting a JSON resource with nested structure from a service and populating a form. Almost the opposite of the post linked to above. I went through the process of ensuring the JSON was valid, the path in the document matched the dot notation of the form and that the field had a value associated with it. The source presented below is a simplified test case designed to isolate the issue. What you see here is a form that is populated when the button is clicked. It looks rather innocent, however the "meh.feh" field doesn't get populated; exactly what I had seen in the original issue.

Simplied Example:

jQuery is treating the "." as CSS notation not as a literal. The solution? Escape them with a double backslash.

Simple fix:

For more information check the jQuery FAQ

2 comments:

  1. Regular expressions FTW!

    function jq(myid) {
    return '#' + myid.replace(/(:|\.)/g,'\\$1');
    }

    $( jq('meh.feh') )

    ReplyDelete
  2. Amazing! We cherished reading through your own publish. We additionally cherished speaking with a person upon Skyp this evening. Many thanks for the records.

    LOL elo boost



    Buy FUT 14 Coins

    ReplyDelete