0

I have a massive long form with about 40 inputs. I want to listen to all focusin events on input fields except ones inside one div with an ID "CxInfo". Is this possible?

Form (Pseudo-Code)

<form>
<input type="text" name="input1" />
<input type="text" name="input2" />
... many more inputs
<div id="CxInfo"> << I want to NOT listen to focusin events on inputs in this div
<input type="text" name="fname" />
<input type="text" name="lname" />
... many more inputs
</div>
</form>

2 Answers 2

1

You can use event delegation to your form element, filtering out events triggered on descendants of #CxInfo:

$(myform).on('focusin', ':not(#CxInfo input)', function(e) {
    /* Your code here */
});

$('#myform').on('focusin', ':not(#CxInfo input)', function(e) {
    this.value = '';
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform">
    <input value="foo" />
    <div id="CxInfo">
        <input  value="bar" />
    </div>
    <input value="baz" />
</form>

Alternatively, you could stop the propagation of the events triggered inside that element:

$('#myform').on('focusin', 'input', function(e) {
    /* Your code here */
});
$('#CxInfo').on('focusin', function(e) {
    e.stopPropagation();
});

$('#myform').on('focusin', 'input', function(e) {
    this.value = '';
});
$('#CxInfo').on('focusin', function(e) {
    e.stopPropagation();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform">
    <input value="foo" />
    <div id="CxInfo">
        <input  value="bar" />
    </div>
    <input value="baz" />
</form>

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

2 Comments

Not working :( $('#Order :text').on('focusin', function (e) { if (e.target.id == "CxInfo") return; $(this).val(""); });
The inputs are inside a div with the id, not the input themselves.
0

I can't test it, but you could use $('Form > input'). On(...) should work as it should only fire for inputs that are a direct child of the form, like your html.

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.