0

This post has been resolved. The answer is:

$("body").on("change", "#formId > input, #formId > select", function () {

My issue :

I use the following to trigger my validation inside one specific form. Now if you notice, I would trigger any input,select on the body. I use body here cause I need the parent element for my jquery to work since the form was not there before.

$("body").on("change", "input, select", function () {

Im looking to link my input and select element to a specific dynamic form that will be load in the body later. Not all the forms. One specific form.

Example to explain but probably not valid of course:

$("body.#formId").on("change", "input, select", function () {

OR

$("body.#formId").on("change", "#formId.input, #formId.select", function () {

That way my event id triggered only for that specific form.

More info :

In between the body that is obviously present on load, and the form that is not, there is a <div id="screen"></div> that is there on load. it's hidden until i feed it with something. But i cant use this one either. All my form fit in there. So I really need to create a chain where I have an element present on the body that will link it to my form id.

MadeInDreams
  • 1,991
  • 5
  • 33
  • 64
  • Very confusing partly because your syntax for selectors is invalid. Selector on the left wrapped in `$()` must be permanent asset in page. Target selectors may certainly be more specific and include a descendant chain although your syntax is incorrect so not sure what structure you want to target – charlietfl Jan 19 '16 at 23:53
  • The first line is valid! I'm looking to link those input and select to a specific form. You mean your not sure what part on the chain i want to target. The upper part of the input and select obviously. i want to assign them a form ID With yes the correct syntax. – MadeInDreams Jan 20 '16 at 00:01
  • @ charlietfl lol you knew that i needed to change 1 dot for a > or just pretended. Cause seriously your comment is like 25 time longer then what could have been an answer. – MadeInDreams Jan 20 '16 at 01:01

1 Answers1

2

I'm not entirely sure I understand your end goal, but I think you're saying that you want to change the context of the first selectors to a dynamic form. This is possibly a duplicate of What is "context" in jQuery selector?

// assuming you are supplied a dynamic formId
$('#' + formId, 'body').on('change', 'input, select', function (e) {});

I've added a link to a working jsFiddle. Please note that I have hardcoded the form ID in my fiddle because you never gave an example of where and how you were receiving that data.

Community
  • 1
  • 1
kaboom
  • 66
  • 4
  • This is exactly what i mean. You example is not working when i use another form i still trigger the event. I want to use one specific form. – MadeInDreams Jan 20 '16 at 00:36
  • 1
    @MadeInDreams Could you post your implementation of my solution? I'm very curious to see why it's failing for you and not me. – kaboom Jan 20 '16 at 01:09
  • well do you have more then one form on your page? and are they dynamically added to the page. Because my forms are not on the page. They are added later. And if i specify the formID with the body like your do it triggers diffrentId – MadeInDreams Jan 20 '16 at 01:15
  • Wow I just retested it and it worked. Well then can you add my solution to yours so you have 2 answers in your post and il mark you as answered – MadeInDreams Jan 20 '16 at 01:19