Hide or show table rows as a hierarchy The 2019 Stack Overflow Developer Survey Results Are...
"Riffle" two strings
Are USB sockets on wall outlets live all the time, even when the switch is off?
Why is it "Tumoren" and not "Tumore"?
How to deal with fear of taking dependencies
Should I write numbers in words or as numerals when there are multiple next to each other?
How to answer pointed "are you quitting" questioning when I don't want them to suspect
Why could you hear an Amstrad CPC working?
How to create dashed lines/arrows in Illustrator
I looked up a future colleague on LinkedIn before I started a job. I told my colleague about it and he seemed surprised. Should I apologize?
If the Wish spell is used to duplicate the effect of Simulacrum, are existing duplicates destroyed?
Can distinct morphisms between curves induce the same morphism on singular cohomology?
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
What is the use of option -o in the useradd command?
JSON.serialize: is it possible to suppress null values of a map?
Extreme, unacceptable situation and I can't attend work tomorrow morning
What is the meaning of Triage in Cybersec world?
What is the steepest angle that a canal can be traversable without locks?
aging parents with no investments
Why Did Howard Stark Use All The Vibranium They Had On A Prototype Shield?
Lethal sonic weapons
How to manage monthly salary
Can't find the latex code for the ⍎ (down tack jot) symbol
How was Skylab's orbit inclination chosen?
Is three citations per paragraph excessive for undergraduate research paper?
Hide or show table rows as a hierarchy
The 2019 Stack Overflow Developer Survey Results Are InQuery, populate, show, hide dataPopulating form contents from GET - especially radio buttonsIs my PHP script/embed remover robust?Abstraction for multiple connection methodsDelos MVC PHP FrameworkSimple JavaScript hide/showHide and show columns in an HTML tableShow/hide descriptionStyle-Changing Handler For a Select BoxAdd Colour Tags on Click - Version 2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
I'm using PHP HTML and JavaScript to write questions that need to be shown/hidden based on inputs from the user. I have several rows in a table, each row contains a <td>
with a question and a <td>
with an input (for the answer to the question) and I am using JavaScript to hide/show rows that contain questions that sometimes aren't applicable to the user if they've answered questions with a certain answer.
For example, Question 1 should always be displayed, Question 2 only displays if the answer to question one is not yes (I have to allow the possibility that the answer is blank). Question 3 should display if Question 2's answer is not yes.
Also, if question 1's answer is changed to something other than yes. Question 2 and 3 should be hidden.
The code works correctly, but is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below? To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
// question asks them if they served in a second branch of the military
// if hideAllQuestions is true, then all questions that rely on this
// question and questions relying on those questions should disappear.
// if showMainQuestion is true, then all immediate follow up questions
var secondBranchLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#secondbranch_row" ];
if (showMainQuestion) {
// functions that cycle through each question in mainQuestionTagID and show them, set them to required, or hide them.
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
// boolean logic that determines if the main question's answer should show/hide the sub questions.
var answer = $("#secondbranch").val();
var hasSecondBranch = ( answer === 'Yes');
var showSubQuestions = (showMainQuestion && hasSecondBranch && !hideAllQuestions);
// all immediate sub questions that should be shown/hidden, but not the sub questions below said sub questions.
var questionTagIDs = [
"#militarybranch2_row",
"#militarybranch2length_row",
"#militarybranch2rank_row",
"#militarybranch2job_row" ];
if (showSubQuestions) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
// boolean logic determining if dischargeLogic should hide/show some questions.
var hideNextQuestions = ( answer === '' || hideAllQuestions );
var showNextMainQuestion = ( !hideNextQuestions);
dischargeLogic(hideNextQuestions, showNextMainQuestion);
};
// question asking if they were discharged honorably
// this function behaves much like the previous one.
var dischargeLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#honorabledischarge_row" ];
if (showMainQuestion) {
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
var answer = $("#honorabledischarge").val();
var answered = (answer !== '');
var questionTagIDs = [ "#dischargedescription_row" ];
if (answered)
{
honorablyDischarged = (answer === 'Yes');
showSubQuestion = (!honorablyDischarged && !hideAllQuestions);
if (showSubQuestion) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
} else {
disableQuestions(questionTagIDs);
}
var hideNextQuestions = ( answer === '' || hideAllQuestions);
var showNextMainQuestion = (!hideNextQuestions);
deploymentsLogic(hideNextQuestions, showNextMainQuestion);
};
Now, here's the PHP/HTML code for it:
<tr style="display:none;" id="secondbranch_row">
<td class="Question">
Have you served in another military branch?
</td>
<td style="width:100px;">
<select type="text" id="secondbranch" name="secondbranch" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2_row">
<td>
Which branch did you serve in?
</td>
<td>
<select type="text" id="militarybranch2" name="militarybranch2" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<!-- all caps means it's a global constant array -->
<?php foreach (MILITARY_BRANCH as $iBranch)
{?>
<option value = "<?php echo $iBranch?>"><?php echo $iBranch?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2length_row">
<td>
How long?
</td>
<td>
<select type="text" id="militarybranch2length" name="militarybranch2length" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<?php foreach (MILITARY_YEARS_SERVED as $iYears)
{?>
<option value = "<?php echo $iYears?>"><?php echo $iYears?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2rank_row">
<td>
What was your rank?
</td>
<td>
<input type="text" id="militarybranch2rank" name="militarybranch2rank" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<tr style="display:none;" id="militarybranch2job_row">
<td>
What was your job?
</td>
<td>
<input type="text" id="militarybranch2job" name="militarybranch2job" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<!-- HONORABLE DISCHARGE -->
<tr style="display:none;" id="honorabledischarge_row">
<td class="Question">
Were you honorably discharged?
</td>
<td style="width:100px;">
<select type="text" id="honorabledischarge" name="honorabledischarge" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="dischargedescription_row">
<td>
What was the discharge?
</td>
<td>
<input type="text" id="dischargedescription" name="dischargedescription" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
javascript php jquery html
$endgroup$
bumped to the homepage by Community♦ 4 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
I'm using PHP HTML and JavaScript to write questions that need to be shown/hidden based on inputs from the user. I have several rows in a table, each row contains a <td>
with a question and a <td>
with an input (for the answer to the question) and I am using JavaScript to hide/show rows that contain questions that sometimes aren't applicable to the user if they've answered questions with a certain answer.
For example, Question 1 should always be displayed, Question 2 only displays if the answer to question one is not yes (I have to allow the possibility that the answer is blank). Question 3 should display if Question 2's answer is not yes.
Also, if question 1's answer is changed to something other than yes. Question 2 and 3 should be hidden.
The code works correctly, but is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below? To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
// question asks them if they served in a second branch of the military
// if hideAllQuestions is true, then all questions that rely on this
// question and questions relying on those questions should disappear.
// if showMainQuestion is true, then all immediate follow up questions
var secondBranchLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#secondbranch_row" ];
if (showMainQuestion) {
// functions that cycle through each question in mainQuestionTagID and show them, set them to required, or hide them.
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
// boolean logic that determines if the main question's answer should show/hide the sub questions.
var answer = $("#secondbranch").val();
var hasSecondBranch = ( answer === 'Yes');
var showSubQuestions = (showMainQuestion && hasSecondBranch && !hideAllQuestions);
// all immediate sub questions that should be shown/hidden, but not the sub questions below said sub questions.
var questionTagIDs = [
"#militarybranch2_row",
"#militarybranch2length_row",
"#militarybranch2rank_row",
"#militarybranch2job_row" ];
if (showSubQuestions) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
// boolean logic determining if dischargeLogic should hide/show some questions.
var hideNextQuestions = ( answer === '' || hideAllQuestions );
var showNextMainQuestion = ( !hideNextQuestions);
dischargeLogic(hideNextQuestions, showNextMainQuestion);
};
// question asking if they were discharged honorably
// this function behaves much like the previous one.
var dischargeLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#honorabledischarge_row" ];
if (showMainQuestion) {
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
var answer = $("#honorabledischarge").val();
var answered = (answer !== '');
var questionTagIDs = [ "#dischargedescription_row" ];
if (answered)
{
honorablyDischarged = (answer === 'Yes');
showSubQuestion = (!honorablyDischarged && !hideAllQuestions);
if (showSubQuestion) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
} else {
disableQuestions(questionTagIDs);
}
var hideNextQuestions = ( answer === '' || hideAllQuestions);
var showNextMainQuestion = (!hideNextQuestions);
deploymentsLogic(hideNextQuestions, showNextMainQuestion);
};
Now, here's the PHP/HTML code for it:
<tr style="display:none;" id="secondbranch_row">
<td class="Question">
Have you served in another military branch?
</td>
<td style="width:100px;">
<select type="text" id="secondbranch" name="secondbranch" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2_row">
<td>
Which branch did you serve in?
</td>
<td>
<select type="text" id="militarybranch2" name="militarybranch2" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<!-- all caps means it's a global constant array -->
<?php foreach (MILITARY_BRANCH as $iBranch)
{?>
<option value = "<?php echo $iBranch?>"><?php echo $iBranch?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2length_row">
<td>
How long?
</td>
<td>
<select type="text" id="militarybranch2length" name="militarybranch2length" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<?php foreach (MILITARY_YEARS_SERVED as $iYears)
{?>
<option value = "<?php echo $iYears?>"><?php echo $iYears?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2rank_row">
<td>
What was your rank?
</td>
<td>
<input type="text" id="militarybranch2rank" name="militarybranch2rank" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<tr style="display:none;" id="militarybranch2job_row">
<td>
What was your job?
</td>
<td>
<input type="text" id="militarybranch2job" name="militarybranch2job" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<!-- HONORABLE DISCHARGE -->
<tr style="display:none;" id="honorabledischarge_row">
<td class="Question">
Were you honorably discharged?
</td>
<td style="width:100px;">
<select type="text" id="honorabledischarge" name="honorabledischarge" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="dischargedescription_row">
<td>
What was the discharge?
</td>
<td>
<input type="text" id="dischargedescription" name="dischargedescription" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
javascript php jquery html
$endgroup$
bumped to the homepage by Community♦ 4 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
$begingroup$
Is the first question “Have you served in another military branch?”? If so, how does it’s containing row havedisplay: none
removed from the style? Also, you stated “Question 3 should display if Question 2's answer is not yes.” but question 2 does not look like a yes/no question...
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 20 at 12:26
add a comment |
$begingroup$
I'm using PHP HTML and JavaScript to write questions that need to be shown/hidden based on inputs from the user. I have several rows in a table, each row contains a <td>
with a question and a <td>
with an input (for the answer to the question) and I am using JavaScript to hide/show rows that contain questions that sometimes aren't applicable to the user if they've answered questions with a certain answer.
For example, Question 1 should always be displayed, Question 2 only displays if the answer to question one is not yes (I have to allow the possibility that the answer is blank). Question 3 should display if Question 2's answer is not yes.
Also, if question 1's answer is changed to something other than yes. Question 2 and 3 should be hidden.
The code works correctly, but is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below? To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
// question asks them if they served in a second branch of the military
// if hideAllQuestions is true, then all questions that rely on this
// question and questions relying on those questions should disappear.
// if showMainQuestion is true, then all immediate follow up questions
var secondBranchLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#secondbranch_row" ];
if (showMainQuestion) {
// functions that cycle through each question in mainQuestionTagID and show them, set them to required, or hide them.
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
// boolean logic that determines if the main question's answer should show/hide the sub questions.
var answer = $("#secondbranch").val();
var hasSecondBranch = ( answer === 'Yes');
var showSubQuestions = (showMainQuestion && hasSecondBranch && !hideAllQuestions);
// all immediate sub questions that should be shown/hidden, but not the sub questions below said sub questions.
var questionTagIDs = [
"#militarybranch2_row",
"#militarybranch2length_row",
"#militarybranch2rank_row",
"#militarybranch2job_row" ];
if (showSubQuestions) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
// boolean logic determining if dischargeLogic should hide/show some questions.
var hideNextQuestions = ( answer === '' || hideAllQuestions );
var showNextMainQuestion = ( !hideNextQuestions);
dischargeLogic(hideNextQuestions, showNextMainQuestion);
};
// question asking if they were discharged honorably
// this function behaves much like the previous one.
var dischargeLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#honorabledischarge_row" ];
if (showMainQuestion) {
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
var answer = $("#honorabledischarge").val();
var answered = (answer !== '');
var questionTagIDs = [ "#dischargedescription_row" ];
if (answered)
{
honorablyDischarged = (answer === 'Yes');
showSubQuestion = (!honorablyDischarged && !hideAllQuestions);
if (showSubQuestion) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
} else {
disableQuestions(questionTagIDs);
}
var hideNextQuestions = ( answer === '' || hideAllQuestions);
var showNextMainQuestion = (!hideNextQuestions);
deploymentsLogic(hideNextQuestions, showNextMainQuestion);
};
Now, here's the PHP/HTML code for it:
<tr style="display:none;" id="secondbranch_row">
<td class="Question">
Have you served in another military branch?
</td>
<td style="width:100px;">
<select type="text" id="secondbranch" name="secondbranch" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2_row">
<td>
Which branch did you serve in?
</td>
<td>
<select type="text" id="militarybranch2" name="militarybranch2" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<!-- all caps means it's a global constant array -->
<?php foreach (MILITARY_BRANCH as $iBranch)
{?>
<option value = "<?php echo $iBranch?>"><?php echo $iBranch?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2length_row">
<td>
How long?
</td>
<td>
<select type="text" id="militarybranch2length" name="militarybranch2length" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<?php foreach (MILITARY_YEARS_SERVED as $iYears)
{?>
<option value = "<?php echo $iYears?>"><?php echo $iYears?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2rank_row">
<td>
What was your rank?
</td>
<td>
<input type="text" id="militarybranch2rank" name="militarybranch2rank" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<tr style="display:none;" id="militarybranch2job_row">
<td>
What was your job?
</td>
<td>
<input type="text" id="militarybranch2job" name="militarybranch2job" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<!-- HONORABLE DISCHARGE -->
<tr style="display:none;" id="honorabledischarge_row">
<td class="Question">
Were you honorably discharged?
</td>
<td style="width:100px;">
<select type="text" id="honorabledischarge" name="honorabledischarge" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="dischargedescription_row">
<td>
What was the discharge?
</td>
<td>
<input type="text" id="dischargedescription" name="dischargedescription" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
javascript php jquery html
$endgroup$
I'm using PHP HTML and JavaScript to write questions that need to be shown/hidden based on inputs from the user. I have several rows in a table, each row contains a <td>
with a question and a <td>
with an input (for the answer to the question) and I am using JavaScript to hide/show rows that contain questions that sometimes aren't applicable to the user if they've answered questions with a certain answer.
For example, Question 1 should always be displayed, Question 2 only displays if the answer to question one is not yes (I have to allow the possibility that the answer is blank). Question 3 should display if Question 2's answer is not yes.
Also, if question 1's answer is changed to something other than yes. Question 2 and 3 should be hidden.
The code works correctly, but is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below? To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
// question asks them if they served in a second branch of the military
// if hideAllQuestions is true, then all questions that rely on this
// question and questions relying on those questions should disappear.
// if showMainQuestion is true, then all immediate follow up questions
var secondBranchLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#secondbranch_row" ];
if (showMainQuestion) {
// functions that cycle through each question in mainQuestionTagID and show them, set them to required, or hide them.
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
// boolean logic that determines if the main question's answer should show/hide the sub questions.
var answer = $("#secondbranch").val();
var hasSecondBranch = ( answer === 'Yes');
var showSubQuestions = (showMainQuestion && hasSecondBranch && !hideAllQuestions);
// all immediate sub questions that should be shown/hidden, but not the sub questions below said sub questions.
var questionTagIDs = [
"#militarybranch2_row",
"#militarybranch2length_row",
"#militarybranch2rank_row",
"#militarybranch2job_row" ];
if (showSubQuestions) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
// boolean logic determining if dischargeLogic should hide/show some questions.
var hideNextQuestions = ( answer === '' || hideAllQuestions );
var showNextMainQuestion = ( !hideNextQuestions);
dischargeLogic(hideNextQuestions, showNextMainQuestion);
};
// question asking if they were discharged honorably
// this function behaves much like the previous one.
var dischargeLogic = function(hideAllQuestions = false, showMainQuestion = true)
{
var mainQuestionTagID = [ "#honorabledischarge_row" ];
if (showMainQuestion) {
enableQuestions(mainQuestionTagID);
requireQuestions(mainQuestionTagID);
} else {
disableQuestions(mainQuestionTagID);
}
var answer = $("#honorabledischarge").val();
var answered = (answer !== '');
var questionTagIDs = [ "#dischargedescription_row" ];
if (answered)
{
honorablyDischarged = (answer === 'Yes');
showSubQuestion = (!honorablyDischarged && !hideAllQuestions);
if (showSubQuestion) {
enableQuestions(questionTagIDs);
requireQuestions(questionTagIDs);
} else {
disableQuestions(questionTagIDs);
}
} else {
disableQuestions(questionTagIDs);
}
var hideNextQuestions = ( answer === '' || hideAllQuestions);
var showNextMainQuestion = (!hideNextQuestions);
deploymentsLogic(hideNextQuestions, showNextMainQuestion);
};
Now, here's the PHP/HTML code for it:
<tr style="display:none;" id="secondbranch_row">
<td class="Question">
Have you served in another military branch?
</td>
<td style="width:100px;">
<select type="text" id="secondbranch" name="secondbranch" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2_row">
<td>
Which branch did you serve in?
</td>
<td>
<select type="text" id="militarybranch2" name="militarybranch2" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<!-- all caps means it's a global constant array -->
<?php foreach (MILITARY_BRANCH as $iBranch)
{?>
<option value = "<?php echo $iBranch?>"><?php echo $iBranch?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2length_row">
<td>
How long?
</td>
<td>
<select type="text" id="militarybranch2length" name="militarybranch2length" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<?php foreach (MILITARY_YEARS_SERVED as $iYears)
{?>
<option value = "<?php echo $iYears?>"><?php echo $iYears?> </option>
<?php }?>
</select>
</td>
</tr>
<tr style="display:none;" id="militarybranch2rank_row">
<td>
What was your rank?
</td>
<td>
<input type="text" id="militarybranch2rank" name="militarybranch2rank" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<tr style="display:none;" id="militarybranch2job_row">
<td>
What was your job?
</td>
<td>
<input type="text" id="militarybranch2job" name="militarybranch2job" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
<!-- HONORABLE DISCHARGE -->
<tr style="display:none;" id="honorabledischarge_row">
<td class="Question">
Were you honorably discharged?
</td>
<td style="width:100px;">
<select type="text" id="honorabledischarge" name="honorabledischarge" style="width:100%;" class="needs_saved_military" required>
<option value=''></option>
<option value='Yes'>Yes</option>
<option value='No'>No</option>
</select>
</td>
</tr>
<tr style="display:none;" id="dischargedescription_row">
<td>
What was the discharge?
</td>
<td>
<input type="text" id="dischargedescription" name="dischargedescription" style="width:100%;" class="needs_saved_military" required></input>
</td>
</tr>
javascript php jquery html
javascript php jquery html
edited Jan 23 '18 at 2:22
Jamal♦
30.6k11121227
30.6k11121227
asked Jan 23 '18 at 2:14
Matt RohdeMatt Rohde
2501514
2501514
bumped to the homepage by Community♦ 4 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 4 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
$begingroup$
Is the first question “Have you served in another military branch?”? If so, how does it’s containing row havedisplay: none
removed from the style? Also, you stated “Question 3 should display if Question 2's answer is not yes.” but question 2 does not look like a yes/no question...
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 20 at 12:26
add a comment |
$begingroup$
Is the first question “Have you served in another military branch?”? If so, how does it’s containing row havedisplay: none
removed from the style? Also, you stated “Question 3 should display if Question 2's answer is not yes.” but question 2 does not look like a yes/no question...
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 20 at 12:26
$begingroup$
Is the first question “Have you served in another military branch?”? If so, how does it’s containing row have
display: none
removed from the style? Also, you stated “Question 3 should display if Question 2's answer is not yes.” but question 2 does not look like a yes/no question...$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 20 at 12:26
$begingroup$
Is the first question “Have you served in another military branch?”? If so, how does it’s containing row have
display: none
removed from the style? Also, you stated “Question 3 should display if Question 2's answer is not yes.” but question 2 does not look like a yes/no question...$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 20 at 12:26
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Your question is about avoiding the javascript being so cumbersome, but I wonder whether once you've worked out an elegant script to write the table, whether elegant javascript code will be easier to work out.
One trick for tables is to create them in entirely in javascript. The following example isn't for the entirety of your table, but it includes some of the sorts of logic I think you would use.
var branches = ["military_branch1","military_branch2"];
military_table_html = '<table>';
for(var i=0; i<branches.length; i++){
military_table_html += "<tr><td>" +branches[i]+"</td></tr>";
if(branches[i] == "military_branch1"){
military_table_html += "<tr><td>that was the first branch</td></tr>";
} else {
military_table_html += "<tr><td>that was the second branch</td></tr>";
}
}
military_table_html += '</table>';
document.getElementById("military_table").innerHTML = military_table_html;
<span id="military_table"></span>
$endgroup$
add a comment |
$begingroup$
is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below?
Yes. Yes there is. Think about the logic at any given question/answer and make it work for every question/answer. The result will be a single function.
We are at question #2:
- Evaluate the previous question's answer:
- NO: display this question
- YES: hide this question
Make that a function. Call it for every question, in order. Every time any question is answered always call it for all questions (in order) as this will handle arbitrarily changed answers.
To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
No. No you don't. Reuse the function; passing in 1) prev. question's answer 2) this question's ID. Perhaps the questions are numbered and that should suffice as an identifier.
Organize your code
- Build the question table separately as @AnthonyHaffey says
- Add an event handler that calls that displayHide function. Every question uses this one handler.
- A javascript array holding all the original questions. Pass that into the table building function.
- Each question has a unique identifier. In the HTML table make an appropriate element property to hold it.
$endgroup$
1
$begingroup$
Downvote elaboration please! If we can't give the OP the resolution (s)he's looking for at least avoid wrong answers. Down votes should not be relative to other answers nor for a non-preferred answer. If my answer is wrong the OP needs to know why/how - and so do I. As of this writing there are only 2 answers, collectively with zero up votes. If code will clarify please say so. If I'm answering a question that was not asked please say so.
$endgroup$
– radarbob
May 14 '18 at 17:52
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185742%2fhide-or-show-table-rows-as-a-hierarchy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Your question is about avoiding the javascript being so cumbersome, but I wonder whether once you've worked out an elegant script to write the table, whether elegant javascript code will be easier to work out.
One trick for tables is to create them in entirely in javascript. The following example isn't for the entirety of your table, but it includes some of the sorts of logic I think you would use.
var branches = ["military_branch1","military_branch2"];
military_table_html = '<table>';
for(var i=0; i<branches.length; i++){
military_table_html += "<tr><td>" +branches[i]+"</td></tr>";
if(branches[i] == "military_branch1"){
military_table_html += "<tr><td>that was the first branch</td></tr>";
} else {
military_table_html += "<tr><td>that was the second branch</td></tr>";
}
}
military_table_html += '</table>';
document.getElementById("military_table").innerHTML = military_table_html;
<span id="military_table"></span>
$endgroup$
add a comment |
$begingroup$
Your question is about avoiding the javascript being so cumbersome, but I wonder whether once you've worked out an elegant script to write the table, whether elegant javascript code will be easier to work out.
One trick for tables is to create them in entirely in javascript. The following example isn't for the entirety of your table, but it includes some of the sorts of logic I think you would use.
var branches = ["military_branch1","military_branch2"];
military_table_html = '<table>';
for(var i=0; i<branches.length; i++){
military_table_html += "<tr><td>" +branches[i]+"</td></tr>";
if(branches[i] == "military_branch1"){
military_table_html += "<tr><td>that was the first branch</td></tr>";
} else {
military_table_html += "<tr><td>that was the second branch</td></tr>";
}
}
military_table_html += '</table>';
document.getElementById("military_table").innerHTML = military_table_html;
<span id="military_table"></span>
$endgroup$
add a comment |
$begingroup$
Your question is about avoiding the javascript being so cumbersome, but I wonder whether once you've worked out an elegant script to write the table, whether elegant javascript code will be easier to work out.
One trick for tables is to create them in entirely in javascript. The following example isn't for the entirety of your table, but it includes some of the sorts of logic I think you would use.
var branches = ["military_branch1","military_branch2"];
military_table_html = '<table>';
for(var i=0; i<branches.length; i++){
military_table_html += "<tr><td>" +branches[i]+"</td></tr>";
if(branches[i] == "military_branch1"){
military_table_html += "<tr><td>that was the first branch</td></tr>";
} else {
military_table_html += "<tr><td>that was the second branch</td></tr>";
}
}
military_table_html += '</table>';
document.getElementById("military_table").innerHTML = military_table_html;
<span id="military_table"></span>
$endgroup$
Your question is about avoiding the javascript being so cumbersome, but I wonder whether once you've worked out an elegant script to write the table, whether elegant javascript code will be easier to work out.
One trick for tables is to create them in entirely in javascript. The following example isn't for the entirety of your table, but it includes some of the sorts of logic I think you would use.
var branches = ["military_branch1","military_branch2"];
military_table_html = '<table>';
for(var i=0; i<branches.length; i++){
military_table_html += "<tr><td>" +branches[i]+"</td></tr>";
if(branches[i] == "military_branch1"){
military_table_html += "<tr><td>that was the first branch</td></tr>";
} else {
military_table_html += "<tr><td>that was the second branch</td></tr>";
}
}
military_table_html += '</table>';
document.getElementById("military_table").innerHTML = military_table_html;
<span id="military_table"></span>
var branches = ["military_branch1","military_branch2"];
military_table_html = '<table>';
for(var i=0; i<branches.length; i++){
military_table_html += "<tr><td>" +branches[i]+"</td></tr>";
if(branches[i] == "military_branch1"){
military_table_html += "<tr><td>that was the first branch</td></tr>";
} else {
military_table_html += "<tr><td>that was the second branch</td></tr>";
}
}
military_table_html += '</table>';
document.getElementById("military_table").innerHTML = military_table_html;
<span id="military_table"></span>
var branches = ["military_branch1","military_branch2"];
military_table_html = '<table>';
for(var i=0; i<branches.length; i++){
military_table_html += "<tr><td>" +branches[i]+"</td></tr>";
if(branches[i] == "military_branch1"){
military_table_html += "<tr><td>that was the first branch</td></tr>";
} else {
military_table_html += "<tr><td>that was the second branch</td></tr>";
}
}
military_table_html += '</table>';
document.getElementById("military_table").innerHTML = military_table_html;
<span id="military_table"></span>
edited May 14 '18 at 17:56
radarbob
5,4601127
5,4601127
answered Jan 23 '18 at 9:05
Anthony HaffeyAnthony Haffey
1336
1336
add a comment |
add a comment |
$begingroup$
is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below?
Yes. Yes there is. Think about the logic at any given question/answer and make it work for every question/answer. The result will be a single function.
We are at question #2:
- Evaluate the previous question's answer:
- NO: display this question
- YES: hide this question
Make that a function. Call it for every question, in order. Every time any question is answered always call it for all questions (in order) as this will handle arbitrarily changed answers.
To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
No. No you don't. Reuse the function; passing in 1) prev. question's answer 2) this question's ID. Perhaps the questions are numbered and that should suffice as an identifier.
Organize your code
- Build the question table separately as @AnthonyHaffey says
- Add an event handler that calls that displayHide function. Every question uses this one handler.
- A javascript array holding all the original questions. Pass that into the table building function.
- Each question has a unique identifier. In the HTML table make an appropriate element property to hold it.
$endgroup$
1
$begingroup$
Downvote elaboration please! If we can't give the OP the resolution (s)he's looking for at least avoid wrong answers. Down votes should not be relative to other answers nor for a non-preferred answer. If my answer is wrong the OP needs to know why/how - and so do I. As of this writing there are only 2 answers, collectively with zero up votes. If code will clarify please say so. If I'm answering a question that was not asked please say so.
$endgroup$
– radarbob
May 14 '18 at 17:52
add a comment |
$begingroup$
is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below?
Yes. Yes there is. Think about the logic at any given question/answer and make it work for every question/answer. The result will be a single function.
We are at question #2:
- Evaluate the previous question's answer:
- NO: display this question
- YES: hide this question
Make that a function. Call it for every question, in order. Every time any question is answered always call it for all questions (in order) as this will handle arbitrarily changed answers.
To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
No. No you don't. Reuse the function; passing in 1) prev. question's answer 2) this question's ID. Perhaps the questions are numbered and that should suffice as an identifier.
Organize your code
- Build the question table separately as @AnthonyHaffey says
- Add an event handler that calls that displayHide function. Every question uses this one handler.
- A javascript array holding all the original questions. Pass that into the table building function.
- Each question has a unique identifier. In the HTML table make an appropriate element property to hold it.
$endgroup$
1
$begingroup$
Downvote elaboration please! If we can't give the OP the resolution (s)he's looking for at least avoid wrong answers. Down votes should not be relative to other answers nor for a non-preferred answer. If my answer is wrong the OP needs to know why/how - and so do I. As of this writing there are only 2 answers, collectively with zero up votes. If code will clarify please say so. If I'm answering a question that was not asked please say so.
$endgroup$
– radarbob
May 14 '18 at 17:52
add a comment |
$begingroup$
is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below?
Yes. Yes there is. Think about the logic at any given question/answer and make it work for every question/answer. The result will be a single function.
We are at question #2:
- Evaluate the previous question's answer:
- NO: display this question
- YES: hide this question
Make that a function. Call it for every question, in order. Every time any question is answered always call it for all questions (in order) as this will handle arbitrarily changed answers.
To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
No. No you don't. Reuse the function; passing in 1) prev. question's answer 2) this question's ID. Perhaps the questions are numbered and that should suffice as an identifier.
Organize your code
- Build the question table separately as @AnthonyHaffey says
- Add an event handler that calls that displayHide function. Every question uses this one handler.
- A javascript array holding all the original questions. Pass that into the table building function.
- Each question has a unique identifier. In the HTML table make an appropriate element property to hold it.
$endgroup$
is there a more efficient method to write the code rather than repeating the cumbersome boolean logic shown below?
Yes. Yes there is. Think about the logic at any given question/answer and make it work for every question/answer. The result will be a single function.
We are at question #2:
- Evaluate the previous question's answer:
- NO: display this question
- YES: hide this question
Make that a function. Call it for every question, in order. Every time any question is answered always call it for all questions (in order) as this will handle arbitrarily changed answers.
To make more questions I need to copy and paste the code and change the boolean conditions and the Element IDs for each question.
No. No you don't. Reuse the function; passing in 1) prev. question's answer 2) this question's ID. Perhaps the questions are numbered and that should suffice as an identifier.
Organize your code
- Build the question table separately as @AnthonyHaffey says
- Add an event handler that calls that displayHide function. Every question uses this one handler.
- A javascript array holding all the original questions. Pass that into the table building function.
- Each question has a unique identifier. In the HTML table make an appropriate element property to hold it.
answered Apr 24 '18 at 2:06
radarbobradarbob
5,4601127
5,4601127
1
$begingroup$
Downvote elaboration please! If we can't give the OP the resolution (s)he's looking for at least avoid wrong answers. Down votes should not be relative to other answers nor for a non-preferred answer. If my answer is wrong the OP needs to know why/how - and so do I. As of this writing there are only 2 answers, collectively with zero up votes. If code will clarify please say so. If I'm answering a question that was not asked please say so.
$endgroup$
– radarbob
May 14 '18 at 17:52
add a comment |
1
$begingroup$
Downvote elaboration please! If we can't give the OP the resolution (s)he's looking for at least avoid wrong answers. Down votes should not be relative to other answers nor for a non-preferred answer. If my answer is wrong the OP needs to know why/how - and so do I. As of this writing there are only 2 answers, collectively with zero up votes. If code will clarify please say so. If I'm answering a question that was not asked please say so.
$endgroup$
– radarbob
May 14 '18 at 17:52
1
1
$begingroup$
Downvote elaboration please! If we can't give the OP the resolution (s)he's looking for at least avoid wrong answers. Down votes should not be relative to other answers nor for a non-preferred answer. If my answer is wrong the OP needs to know why/how - and so do I. As of this writing there are only 2 answers, collectively with zero up votes. If code will clarify please say so. If I'm answering a question that was not asked please say so.
$endgroup$
– radarbob
May 14 '18 at 17:52
$begingroup$
Downvote elaboration please! If we can't give the OP the resolution (s)he's looking for at least avoid wrong answers. Down votes should not be relative to other answers nor for a non-preferred answer. If my answer is wrong the OP needs to know why/how - and so do I. As of this writing there are only 2 answers, collectively with zero up votes. If code will clarify please say so. If I'm answering a question that was not asked please say so.
$endgroup$
– radarbob
May 14 '18 at 17:52
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185742%2fhide-or-show-table-rows-as-a-hierarchy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Is the first question “Have you served in another military branch?”? If so, how does it’s containing row have
display: none
removed from the style? Also, you stated “Question 3 should display if Question 2's answer is not yes.” but question 2 does not look like a yes/no question...$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 20 at 12:26