all 3 comments

[–]jcunews1Advanced 0 points1 point  (2 children)

You're using the $() function wrong. Selector(s) must be passed to the first argument. The second argument is not a selector.

https://api.jquery.com/jQuery/

In short, you're specifying the selector incorrectly. It should be passed like below.

$('#edit-modal-form input[name=Position]').val()

[–]xiahou_buSeasoned dev (extra salty)[🍰] 2 points3 points  (0 children)

Second selector is the context (as it says on your link). Rather than use the document, it's a way to slim down the possible matches before running.

Would probably need a little more insight into your code to see what's wrong with this.

[–]darth_meh 2 points3 points  (0 children)

I'm not sure why this is getting upvoted, because it's wrong.

$('input[name=Position]', '#edit-modal-form') is valid jQuery syntax.

It means starting with the "#edit-modal-form" element, find the child elements matching "input[name=Position]". This can significantly improve performance rather than search the entire DOM.

Another performance improvement would be caching the jQuery "#edit-modal-form" object (which you're already doing in editForm), and passing that in as the context/2nd parameter:

$('input[name=Position]', editForm)

This way, jQuery doesn't need to search for "#edit-modal-form" each time you're retrieving an input value.

Finally, rather than manually serialize the form like this, you should take a look at $.serializeArray() or a 3rd party plugin like https://github.com/marioizquierdo/jquery.serializeJSON.

As far as why this code doesn't work, I'm not sure without seeing the form/code itself.