I have already gone through questions available on this topic and have tried everything, but still my keyup function is not working.
JavaScript
x
34
34
1
$(document).ready(function() {
2
3
$(document).on('keyup', '.pollOption', function() {
4
var empty = false;
5
$(".pollOption").each(function() {
6
if ($(this).val() == '') {
7
empty = true;
8
}
9
});
10
11
if (empty) {
12
$("#cpsubmit").attr('disabled', 'disabled');
13
$("#moreop").attr('disabled', 'disabled');
14
} else {
15
$("#cpsubmit").removeAttr('disabled');
16
$("#moreop").removeAttr('disabled');
17
}
18
19
});
20
//Keep Track of no. of options on the page
21
var noOfOptions = 2;
22
// Function to add input fields (since I may have to delete them I've use bootstrap's input-groups, I guess this is causing issue)
23
$("#moreop").on('click', function() {
24
noOfOptions++;
25
$("#options").append("<div class='input-group pollOption'><input class='form-control' type='text' placeholder='New Option' name='op" + noOfOptions + "'/><span class='input-group-addon'><a href='#' id='removeOption' class='text-danger'>Remove</a></span></div>");
26
});
27
28
// To delete any option (only the dynamically created options can be deleted)
29
$("#cpform").on('click', '#removeOption', function() {
30
$(this).parents('.input-group').remove();
31
noOfOptions--;
32
});
33
34
});
JavaScript
1
14
14
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
2
<form id="cpform" method="POST" action="/polls/add">
3
<div class="form-group">
4
<label>Title</label>
5
<input id="title" type="text" placeholder="Ask your question here..." name="title" class="form-control" />
6
</div>
7
<div id="options" class="form-group">
8
<label>Options</label>
9
<input type="text" placeholder="Option 1" name="op1" class="form-control pollOption" />
10
<input type="text" placeholder="Option 2" name="op2" class="form-control pollOption" />
11
</div>
12
<button id="moreop" type="button" disabled="disabled" class="btn btn-outline-info btn-primary">More Options</button><br/><br/>
13
<button id="cpsubmit" type="submit" disabled="disabled" class="btn btn-info btn-primary">Submit</button>
14
</form>
This code works perfectly for the two inputs already in the HTML part. When I click on the “More Option” button the new field gets added but the “keyup” does not work on it. In fact, when I enter something on the new added inputs then my “More Option” & “Submit” button gets disabled (really do’t know why this is happening).
Advertisement
Answer
You’ve to add the class pollOption
to the input
and not the div
in your append :
JavaScript
1
3
1
$("#options").append("<div class='input-group'><input class='pollOption form-control' ...
2
_____________________________________________________________^^^^^^^^^^
3
Instead of :
JavaScript
1
3
1
$("#options").append("<div class='input-group pollOption'><input class='form-control' ...
2
______________________________________________^^^^^^^^^^
3
Demo:
JavaScript
1
35
35
1
$(document).ready(function() {
2
3
$(document).on('keyup', '.pollOption', function() {
4
var empty = false;
5
$(".pollOption").each(function() {
6
if ($(this).val() == '') {
7
empty = true;
8
}
9
});
10
11
if (empty) {
12
$("#cpsubmit").attr('disabled', 'disabled');
13
$("#moreop").attr('disabled', 'disabled');
14
} else {
15
$("#cpsubmit").removeAttr('disabled');
16
$("#moreop").removeAttr('disabled');
17
}
18
19
});
20
//Keep Track of no. of options on the page
21
var noOfOptions = 2;
22
// Function to add input fields (since I may have to delete them I've use bootstrap's input-groups, I guess this is causing issue)
23
$("#moreop").on('click', function() {
24
noOfOptions++;
25
$("#options").append("<div class='input-group'><input class='pollOption form-control' type='text' placeholder='New Option' name='op" + noOfOptions + "'/><span class='input-group-addon'><a href='#' id='removeOption' class='text-danger'>Remove</a></span></div>");
26
$(this).attr('disabled','disaled');
27
});
28
29
// To delete any option (only the dynamically created options can be deleted)
30
$("#cpform").on('click', '#removeOption', function() {
31
$(this).parents('.input-group').remove();
32
noOfOptions--;
33
});
34
35
});
JavaScript
1
15
15
1
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
2
3
<form id="cpform" method="POST" action="/polls/add">
4
<div class="form-group">
5
<label>Title</label>
6
<input id="title" type="text" placeholder="Ask your question here..." name="title" class="form-control" />
7
</div>
8
<div id="options" class="form-group">
9
<label>Options</label>
10
<input type="text" placeholder="Option 1" name="op1" class="form-control pollOption" />
11
<input type="text" placeholder="Option 2" name="op2" class="form-control pollOption" />
12
</div>
13
<button id="moreop" type="button" disabled="disabled" class="btn btn-outline-info btn-primary">More Options</button><br/><br/>
14
<button id="cpsubmit" type="submit" disabled="disabled" class="btn btn-info btn-primary">Submit</button>
15
</form>