var cnt = 2;
$('.next').click(function() {
var reqField = true;
$(this).siblings('table').find('tbody tr:nth-child(' + cnt + ') td').each(function() {
var ele = $(this).find('center').find('select');
if ((!$(ele).attr('disabled')) && $(ele).val() === '') {
reqField = false;
}
});
if (reqField) {
$(this).parent().hide().next().show();
cnt = cnt + 2;
}
});
$('select').change(function() {
if ($(this).val() == 'MSA')
$(this).closest('td').next().find('select').prop('disabled', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
<form action="ServletExample" method="post">
<div id="div1">
<table id="t01" border="2">
<tbody>
<tr>
<td colspan="3">Welcome1</td>
</tr>
<tr>
<td width=15%>
<center><select id="L1" required>
<option value="">Selection1</option>
<option value="MSA">MSA</option>
<option value="dialect">Dialect</option>
</select></center>
</td>
<td width=12%>
<center><select id="D1" required>
<option value="">Selection2</option>
<option value="EGY">EGY</option>
<option value="GLF">GLF</option>
</select></center>
</td>
<td>
<input type="checkbox" id="myCheck1" value="sentence" /> sentence1<br>
<input type="checkbox" id="myCheck2" value=" words" />words1
<textarea name="T" id="myText13" rows="3" cols="50"></textarea>
</td>
</tr>
<tr>
<td colspan="3">Welcome2</td>
</tr>
<tr>
<td width=15%>
<center><select id="L2" required>
<option value="">Selection1</option>
<option value="MSA">MSA</option>
<option value="dialect">Dialect</option>
</select></center>
</td>
<td width=12%>
<center><select id="D2" required>
<option value="">Selection2</option>
<option value="EGY">EGY</option>
<option value="GLF">GLF</option>
</select></center>
</td>
<td>
<input type="checkbox" id="myCheck3" value="sentence" /> sentence2<br>
<input type="checkbox" id="myCheck4" value=" words" />words2
<textarea name="T" id="myText13" rows="3" cols="50"></textarea>
</td>
</tr>
</tbody>
</table>
<hr />
<button class="next">NEXT</button>
</div>
<div id="div2" style="display:none">
<table id="t01" border="2">
<tbody>
<tr>
<td colspan="3">Welcome3</td>
</tr>
<tr>
<td>
<center><select id="L3" required>
<option value="">Selection3</option>
<option value="MSA">MSA</option>
<option value="dialect">Dialect</option>
</select></center>
</td>
<td>
<center><select id="D3" required>
<option value="">Selection4</option>
<option value="EGY">EGY</option>
<option value="GLF">GLF</option>
</select></center>
</td>
<td>
<input type="checkbox" id="myCheck5" value="sentence" /> sentence3<br>
<input type="checkbox" id="myCheck6" value=" words" />words3
<textarea name="T" id="myText13" rows="3" cols="50"></textarea>
</td>
</tr>
</tbody>
</table>
<hr />
<button class="next">NEXT</button>
</div>
<div id="div3" style="display:none">
<table id="t01" border="2">
<tbody>
<tr>
<td colspan="3">Welcome4</td>
</tr>
<tr>
<td>
<center><select id="L4" required>
<option value="">Selection5</option>
<option value="MSA">MSA</option>
<option value="dialect">Dialect</option>
</select></center>
</td>
<td>
<center><select id="D4" required>
<option value="">Selection6</option>
<option value="EGY">EGY</option>
<option value="GLF">GLF</option>
</select></center>
</td>
<td>
<input type="checkbox" id="myCheck7" value="sentence" /> sentence4<br>
<input type="checkbox" id="myCheck8" value=" words" />words4
<textarea name="T" id="myText13" rows="3" cols="50"></textarea>
</td>
</tr>
</tbody>
</table>
<button onclick="submit">Send</button>
</div>
</form>
</div>
I want to hide a div and show another div. I used jQuery and the hide step is fine:
<body>
<div id="div1">
<select id="L1" required>
<option value="0">please select</option>
<option value="A">A</option>
<option value="B">B</option>
</select>
<select id="C1" required>
<option value="0">please select</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<select id="D1" required>
<option value="0">please select</option>
<option value="E">E</option>
<option value="F">F</option>
</select>
<button class="next">next</button>
</div>
<div id="div2" style="display:none">
<select id="D2" required>
<option value="0">please select</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<button class="next">next</button>
</div>
<script>
$('.next').click(function(){
$(this).parent().hide().next().show(); //hide parent and show next
});
</script>
</body>
But the problem is that the div can hide even if some required fields are not selected.
I have 5 dropdown select options in each div and users must select an option for each dropdown before the div is hidden, so if the Next button in the first div is clicked, it must check that all the required fields are selected, and then hide the first div and show the second div.
How I can check this?
I got the problem, it is in the column tag , because I designed the webpage as a table and put every select options in a column.
<td><select id="C1" required>
<option value="0">please select</option>
<option value="C">C</option>
<option value="D">D</option>
</select></td>
so, how I can check the select inside a td tag. Below is the structure of HTML
<html>
<body>
<div id="main">
<form action="ServletExample" method="post">
<div id="div1">
<table id="t01" border="2">
<tbody>
<tr>
<td>
<center><select id="L1" required>
<option value="">Selection1</option>
<option value="MSA">MSA</option>
<option value="dialect">Dialect</option>
</select></center>
</td>
<td>
<center><select id="D1" required>
<option value="">Selection2</option>
<option value="EGY">EGY</option>
<option value="GLF">GLF</option>
</select></center>
</td>
</tr>
</tbody>
</table>
<hr />
<button class="next">NEXT</button>
</div>
<div id="div2" style="display:none">
<table id="t01" border="2">
<tbody>
<tr>
<td>
<center><select id="L2" required>
<option value="">Selection1</option>
<option value="MSA">MSA</option>
<option value="dialect">Dialect</option>
</select></center>
</td>
<td>
<center><select id="D2" required>
<option value="">Selection2</option>
<option value="EGY">EGY</option>
<option value="GLF">GLF</option>
</select></center>
</td>
</tr>
</tbody>
</table>
<hr />
<button class="next">NEXT</button>
</div>
<div id="div3" style="display:none">
<table id="t01" border="2">
<tbody>
<tr>
<td>
<center><select id="L3" required>
<option value="">Selection1</option>
<option value="MSA">MSA</option>
<option value="dialect">Dialect</option>
</select></center>
</td>
<td>
<center><select id="D3" required>
<option value="">Selection2</option>
<option value="EGY">EGY</option>
<option value="GLF">GLF</option>
</select></center>
</td>
</tr>
</tbody>
</table>
<button onclick="submit">Send</button>
</div>
</form>
</div>
<script type="text/javascript">
</script>
</body>
</html>