Access and Display post values on a html page using JQuery - javascript

I am trying to display values in table cells in a form after they are posted by another form. Below is my code for that. Here I am trying to add the value of Select box in unbound_AdultVal hidden field using JQuery.
<form enctype="multipart/form-data" method="post" action="summary.html" name="form">
<input id="unbound_CurrentStepID" type="hidden" value="Step_Two" name="unbound_CurrentStepID">
<input id="Unbound_AdultVal" type="hidden" value="" name="Unbound_AdultVal">
Ages 10+:
<select id="Unbound_Ticket" name="Unbound_Ticket">
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<input type="submit" value="Continue" name="submit">
</form>
<script type="text/javascript">
$(document).ready(function() {
var numDAdult = $("#Unbound_Ticket").val();
$("#Unbound_Ticket").change( function(){
numDAdult = $("#Unbound_Ticket").val();
$('input[name=Unbound_AdultVal]').val(numDAdult);
});
});
</script>
Below is Summary.html code where I want to display the hidden field value.
<form enctype="multipart/form-data" method="post" action="summary.html" name="form">
<input id="unbound_CurrentStepID" type="hidden" value="Form_Three" name="unbound_CurrentStepID">
<table width="800" border="1" align="center" cellpadding="5" cellspacing="0">
<tr>
<td width="381"><strong>Quantity</strong></td>
<td width="242"><strong>Ticket Price</strong></td>
<td width="139"><strong>Total Price</strong></td>
</tr>
<tr>
<td><span id="postAdultvalue"></span>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="3" align="right"><strong>Current Amount Due: $0</strong></td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function() {
$("#postAdultvalue").text($("#Unbound_AdultVal").val());
});
</script>
I am having a hard time displaying posted values in table using JQuery. As I am very new to JQuery please pardon my ignorance.

<input id="Unbound_AdultVal" type="hidden" **value=""** name="Unbound_AdultVal">
The value needs to be set to the POST variable (ex. PHP: $_POST["name"], ColdFusion: #form.name#).

Related

How to remove table row if the form any of the input or select was empty?

I have a form as below, Here i am looking to remove tr row if the input text and select option was empty. and the show the table row if any of the input was not empty
As you can see the input was not empty, it was removing the entire row, it is possible check both are not empty then remove
By using Jquery
Note: table rows are dynamically adding
$(function(){
$("table#my_form tr td").each(function(){
$(this).find('input, select').each(function(){
if($(this).val()==""){
$(this).closest('tr.table_row').remove();
}
})
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST" action="">
<table id="my_form">
<tr class="table_row">
<td>
<input type="text" name="company" value="TCS" />
</td>
<td>
<select name="favorites" id="favorites">
<option value="">Select Favorites</option>
<option value="chess">Chess</option>
<option value="caroms" selected>Caroms</option>
<option value="ruby">Ruby</option>
</select>
</td>
</tr>
<tr class="table_row">
<td>
<input type="text" name="company" value="Deloite" />
</td>
<td>
<select name="favorites" id="favorites">
<option value="">Select Favorites</option>
<option value="chess">Chess</option>
<option value="caroms">Caroms</option>
<option value="ruby">Ruby</option>
</select>
</td>
</tr>
<tr class="table_row">
<td>
<input type="text" name="company" value="GOOGLE" />
</td>
<td>
<select name="favorites" id="favorites">
<option value="">Select Favorites</option>
<option value="chess" selected>Chess</option>
<option value="caroms">Caroms</option>
<option value="ruby">Ruby</option>
</select>
</td>
</tr>
</table>
</form>
To check all inputs, you could try something like this. As you mentioned, you are adding rows dynamically, so you would want some other event handler to check again for more bad rows. This code would run once on page load, and then again any time the '#removeBadRows' button is clicked.
$(function () {
removeBadRows();
$(document).on('click', '#removeBadRows', function () {
removeBadRows();
});
});
function removeBadRows() {
$("table#my_form tr").each(function () {
var badCount = 0;
var inputLength = $(this).find('input, select').length;
$(this).find('input, select').each(function () {
if ($(this).val() == "") {
badCount++;
}
});
if (badCount == inputLength) {
$(this).remove();
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST" action="">
<table id="my_form">
<tr class="table_row">
<td>
<input type="text" name="company" value="test" />
</td>
<td>
<select name="favorites" id="favorites">
<option value="">Select Favorites</option>
<option value="chess">Chess</option>
<option value="caroms" selected>Caroms</option>
<option value="ruby">Ruby</option>
</select>
</td>
</tr>
<tr class="table_row">
<td>
<input type="text" name="company" value="" />
</td>
<td>
<select name="favorites" id="favorites">
<option value="" selected>Select Favorites</option>
<option value="chess">Chess</option>
<option value="caroms">Caroms</option>
<option value="ruby">Ruby</option>
</select>
</td>
</tr>
<tr class="table_row">
<td>
<input type="text" name="company" value="GOOGLE" />
</td>
<td>
<select name="favorites" id="favorites">
<option value="">Select Favorites</option>
<option value="chess">Chess</option>
<option value="caroms">Caroms</option>
<option value="ruby" selected>Ruby</option>
</select>
</td>
</tr>
</table>
</form>
This could be the most simple method
$(function(){
$(document).on('change', '.table_row input', function(e) {
if($(this).val()==""){
$(this).closest('.table_row').remove();
}
});
});

How to get values of a form using JavaScript

I'm trying to write a script to process form data within a simple offline HTML page. I lifted the first four variables I'm trying to pull through as an example. I can get a simple example to work like the one found here https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_form_elements_index but I'm struggling to expand it to multiple variables as below.
Am I approaching the two JavaScript functions wrong for this use?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript within HTML</title>
<script language = "JavaScript">
function get_dropdown (fieldname) {
Item = form.fieldname.selectedIndex;
Result = form.fieldname.options[Item].text;
return Result;
}
function getval (form) {
var Customer_Name = form.customer_name.value;
var Asset_Name = form.asset_name.value;
var Asset_Attribute = get_dropdown (asset_dropdown);
var Sub_Attribute = get_dropdown (sub_dropdown);
var Answer = Customer_Name + Asset_Name + Asset_Type + Sub_System;
document.getElementById("output1").innerHTML = Answer;
}
</script>
</head>
<body>
<form name = "agc_ira" method="get" action="" >
<fieldset>
<legend>Assessment Data</legend>
<table>
<tr>
<td>Customer Name:</td>
<td><input type="text" name="customer_name"></td>
</tr>
<tr>
<td>Asset Name:</td>
<td><input type = "text" name="asset_name"></td>
</tr>
<tr>
<td>Asset Attribute:</td>
<td><select name="asset_dropdown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select></td>
</tr>
<tr>
<td>Sub-Attribute:</td>
<td><select name="sub_dropdown">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select></td>
</tr>
</table>
</fieldset>
<br><input type="button" name="runmodel" value="Run" onclick="getval()">
<br>
<p id="output1"></p>
</form>
</body>
</html>
There are a few problems with your code that I will do my best to explain. It seems like you are a little confused with the form object and how that works. Firstly, it is forms not form. The reason its plural is because it is an array that contains all of the <form> elements in the document. Secondly, you need to access it by prefixing it with document because the forms live inside the document object. So all your calls to form need to updated to use document.forms
Next is how you are dynamically reaching into the form object. The syntax for that would be document.forms[0][variablename]. Let's break that down piece by piece so you can understand better. document.forms is a reference to all the forms in the document. [0] is taking the first item in that array and because the form itself is another array we use [variableName] to pull out the value we want.
After that, there is just some small confusion over variable names which you should be able to see from the working example I've provided below.
function get_dropdown(fieldname) {
Item = document.forms[0][fieldname].selectedIndex;
Result = document.forms[0][fieldname].options[Item].text;
return Result;
}
function getval(form) {
var Customer_Name = document.forms[0].customer_name.value;
var Asset_Name = document.forms[0].asset_name.value;
var Asset_Attribute = get_dropdown('asset_dropdown');
var Sub_Attribute = get_dropdown('sub_dropdown');
var Answer = Customer_Name + '-' + Asset_Name + '-' + Asset_Attribute + '-' + Sub_Attribute;
document.getElementById("output1").innerHTML = Answer;
}
<form name="agc_ira" method="get" action="">
<fieldset>
<legend>Assessment Data</legend>
<table>
<tr>
<td>Customer Name:</td>
<td><input type="text" name="customer_name"></td>
</tr>
<tr>
<td>Asset Name:</td>
<td><input type="text" name="asset_name"></td>
</tr>
<tr>
<td>Asset Attribute:</td>
<td><select name="asset_dropdown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select></td>
</tr>
<tr>
<td>Sub-Attribute:</td>
<td><select name="sub_dropdown">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select></td>
</tr>
</table>
</fieldset>
<br><input type="button" name="runmodel" value="Run" onclick="getval()">
<br>
<p id="output1"></p>
</form>
Please feel free to ask more follow up questions if I've missed something or you need more clarification.
Generic solution for multiple forms
With elements collection one can access all elements in a form. Loop through them and check the type of each element and make your stuff with it.
Example
function getSelected(formName, fieldName) {
var selected = document.forms[formName][fieldName].selectedIndex;
var res = document.forms[formName][fieldName].options[selected].text;
return res;
}
function getValuesOfForm(formName, outputId) {
var res = "";
var formElements = document.forms[formName].elements;
for (var i = 0; i < formElements.length; i += 1) {
if (formElements[i].type === "text") {
res += formElements[i].value + "-";
}
if (formElements[i].type === "select-one") {
res += getSelected(formName, formElements[i].name) + "-";
}
// Do your own stuff with other types
}
var output = document.getElementById(outputId);
output.innerHTML = res.slice(0, -1);
}
<!-- Form 1 -->
<form name="form1" method="get" action="">
<fieldset>
<legend>Assessment Data</legend>
<table>
<tr>
<td>Customer Name:</td>
<td><input type="text" name="customer_name"></td>
</tr>
<tr>
<td>Asset Name:</td>
<td><input type="text" name="asset_name"></td>
</tr>
<tr>
<td>Asset Attribute:</td>
<td>
<select name="asset_dropdown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
<tr>
<td>Sub-Attribute:</td>
<td>
<select name="sub_dropdown">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
</tr>
</table>
</fieldset>
<br><input type="button" name="runmodel" value="Run" onclick="getValuesOfForm('form1', 'output1')">
<br>
<p id="output1"></p>
</form>
<!-- Form 2 -->
<form name="form2" method="get" action="">
<fieldset>
<legend>Assessment Data</legend>
<table>
<tr>
<td>Customer Name:</td>
<td><input type="text" name="customer_name"></td>
</tr>
<tr>
<td>Another Text:</td>
<td><input type="text" name="another_text"></td>
</tr>
<tr>
<td>Asset Name:</td>
<td><input type="text" name="asset_name"></td>
</tr>
<tr>
<td>Asset Attribute:</td>
<td>
<select name="asset_dropdown">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
<tr>
<td>Sub-Attribute:</td>
<td>
<select name="sub_dropdown">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</td>
</tr>
<tr>
<td>Other attributes:</td>
<td>
<select name="other_attributes">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
</td>
</tr>
</table>
</fieldset>
<br><input type="button" name="runmodel" value="Run" onclick="getValuesOfForm('form2', 'output2')">
<br>
<p id="output2"></p>
</form>

Accessing parent select value in pop-window or save into a temp table

I have a number of select as below.
<form method="post" name="form1" action="addpp.php" enctype="multipart/form-data" onSubmit="return validateForm();">
<table width="100%">
<tr>
<td>
<select name="leftProcess" size="5">
<option value="1">Process 1</option>
<option value="2">Process 2</option>
<option value="3">Process 3</option>
</select>
</td>
<td>
<button onclick="moveRight('leftProcess','rightProcess')">>></button><br/>
<button onclick="moveLeft('rightProcess','leftProcess')"><<</button>
</td>
<td>
<select name="rightProcess" size="5">
</select>
</td>
</tr>
<tr>
Sub process for Process 1
<td>
<select name="leftSubProcess1" size="5">
<option value="1">Sub 1_1</option>
<option value="2">Sub 1_2</option>
<option value="3">Sub 1_3</option>
</select>
</td>
<td>
<button onclick="moveRight('leftSubProcess1','rightSubProcess1')">>></button><br/>
<button onclick="moveLeft('rightSubProcess1','leftSubProcess1')"><<</button>
</td>
<td>
<select name="rightSubProcess1" size="5">
</select>
</td>
</tr>
<tr>
Sub process for Process 2
<td>
<select name="leftSubProcess2" size="5">
<option value="1">Sub 2_1</option>
<option value="2">Sub 2_2</option>
<option value="3">Sub 2_3</option>
</select>
</td>
<td>
<button onclick="moveRight('leftSubProcess2','rightSubProcess2')">>></button><br/>
<button onclick="moveLeft('rightSubProcess2','leftSubProcess2')"><<</button>
</td>
<td>
<select name="rightSubProcess2" size="5">
</select>
</td>
</tr>
<tr>
Sub process for Process 3
<td>
<select name="leftSubProcess3" size="5">
<option value="1">Sub 3_1</option>
<option value="2">Sub 3_2</option>
<option value="3">Sub 3_3</option>
</select>
</td>
<td>
<button onclick="moveRight('leftSubProcess3','rightSubProcess3')">>></button><br/>
<button onclick="moveLeft('rightSubProcess3','leftSubProcess3')"><<</button>
</td>
<td>
<select name="rightSubProcess3" size="5">
</select>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<input type="submit" name="Add" value="Add" style="width:80px; height:25px;"></input>
<input type="button" name="Preview" onclick="javascript:openChildWindow();" value="Preview" style="width:80px; height:25px;"/>
<input type="reset" name="reset" value="Cancel" style="width:80px; height:25px;"/>
</td>
</tr>
</table>
</form>
The first select will be all the process so user can choose and push them to the right select box. Accordingly the 3 below select are for sub process for the all the process above. User can also pick and choose which one they would desire.Once I cick the preview button I would the user to view their selection in this format. For e.g. use pickProcess 3 followed by Process 2 and Process 1. So then below must first show all the sub_proces3 then sub_process2 and sub_process1. What is best was to select and show these sort of data must I like save into a temp table? The idea is to give them a preview what they have select before the final submission into database.

JSP multiple checkbox unable to capture multiple row values in a dynamic table

<input type="submit" id="Display" name="Display" value="Display" />
<br/><br/>
<input type="submit" id="Update" name="Update" value="Update" />
<br/><br/>
<table border=1 width=90% height=30% align=center cellpadding=1 cellspacing=1>
<tr>
<th>Select</th>
<th>Name</th>
<th>Current Value</th>
<th>New Value</th>
</tr>
<c:forEach items="${abc}" var="map">
<tr>
<td align="center"><input type="checkbox" id="selectedItems" value="${map.key}" name="selectedItems" />
</td>
<td align="center">${map.key}</td>
<td align="center">${map.value}</td>
<td align="center">
<select name="val" id="val" >
<option value="">Select New Value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
</c:forEach>
</table>
I am constructing a dynamic table based upon the values I get in map object 'abc' on click of Display Button. Now the next step is to check one or more rows and update them by selecting a new value from dropdown. How can I capture the ${map.key} and the selected new value per row in a new map on click of 'Update'? I am able to capture the checked ${map.key} but not the corresponding selected new value from dropdown. Any help would be appreciated.
This will do i believe..!!
<c:forEach items="${abc}" var="map" varStatus="rowNumber">
<tr>
<td align="center"><input type="checkbox" id="selectedItems${rowNumber.index}" value="${map.key}" name="selectedItems[${rowNumber.index}]" />
</td>
<td align="center">${map.key}</td>
<td align="center">${map.value}</td>
<td align="center">
<select name="val" id="val" >
<option value="">Select New Value</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
</c:forEach>
The idea is that you must use some array for storing value for each
checkbox. By giving the name as selectedItems[${rowNumber.index}]
you can save the value of first checkbox at selectedItems[0], 2nd
at 1 etc. All you need to do is to create an array inside the bean an
create getters and setters.
varStatus="rowNumber" is used to get the current iteration index,
like i in the for loop. You can use this index to differentiate each
row.
<input type="submit" id="Display" name="Display" value="Display" />
<br/><br/>
<input type="submit" id="Update" name="Update" value="Update" />
<br/><br/>
<table border=1 width=90% height=30% align=center cellpadding=1 cellspacing=1>
<tr>
<th>Select</th>
<th>Name</th>
<th>Current Value</th>
<th>New Value</th>
</tr>
<c:forEach items="${abc}" var="map" varStatus="rowNumber">
<tr>
<td align="center"><input type="checkbox" id="selectedItems" value="${map.key}=${rowNumber.index}" name="selectedItems" />
</td>
<td align="center">${map.key}</td>
<td align="center">${map.value}</td>
<td align="center">
<select name="val" id="val" >
<option value="=${rowNumber.index}">Select New Value</option>
<option value="1=${rowNumber.index}">1</option>
<option value="2=${rowNumber.index}">2</option>
<option value="3=${rowNumber.index}">3</option>
</select>
</td>
</tr>
</c:forEach>
</table>
Thank you Dileep, you idea helped. Here is how I did it. Now I can track for a particular row, what dropdown value was selected and create a map from it.

Hide input boxes based on drop down selection

I want a drop down menu at the top of the page to determine how many boxes are then showing on the page.
If the user selects 1, only 1 table shows
If the user selects 2, 2 tables show
I have added the code so hopefully it makes more sense
<body>
<p>Please select number of puppies:</p>
<p>
<select>
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<form id="form1" name="form1" method="POST" action="<%=MM_editAction%>">
<p>Puppy 1: </p>
<p> </p>
<table width="330" border="0">
<tr>
<td>Name: </td>
<td><input type="text" name="PuppyName1" /></td>
</tr>
<tr>
<td>Colour: </td>
<td><input type="text" name="PuppyColour1" /></td>
</tr>
<tr>
<td>Sex:</td>
<td>
<select name="PuppySex1" id="PuppySex1">
<option value=" "> </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
<tr>
<td>Microchip/DNA Profile:</td>
<td>
<select name="PuppyMicrochip1" id="PuppyMicrochip1">
<option value="No">No</option>
<option value="Microchip">Microchip</option>
<option value="DNA Profile">DNA Profile</option>
</select>
</td>
</tr>
<tr>
<td><p>Microchip/DNA Number:</p></td>
<td> </td>
</tr>
</table>
<p>Puppy 2:</p>
<table width="330" border="0">
<tr>
<td>Name: </td>
<td><input type="text" name="PuppyName2" /></td>
</tr>
<tr>
<td>Colour: </td>
<td><input type="text" name="PuppyColour2" /></td>
</tr>
<tr>
<td>Sex:</td>
<td>
<select name="PuppySex2" id="PuppySex2">
<option value=" "> </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
<tr>
<td>Microchip/DNA Profile:</td>
<td>
<select name="PuppyMicrochip2" id="select2">
<option value="No">No</option>
<option value="Microchip">Microchip</option>
<option value="DNA Profile">DNA Profile</option>
</select>
</td>
</tr>
<tr>
<td><p>Microchip/DNA Number:</p></td>
<td>
<input name="PuppyMicrochipNum2" type="text"
id="PuppyMicrochipNum2" />
</td>
</tr>
</table>
jsFiddle: http://jsfiddle.net/h3XLP/
very common to get jQuery answers but it's really not that comprehensive with standalone JavaScript
note: add the attribute style="display:none;" to the second table
var select = document.getElementsByTagName("select")[0];
select.onchange=function(){
if(select.value=="2"){
document.getElementsByTagName("table")[1].style.display="inline";
}else{
document.getElementsByTagName("table")[1].style.display="none";
}
}
however you should alternatively use below, as you may have more select and table elements in your document
http://jsfiddle.net/h3XLP/1/
var select = document.getElementById("selectnopuppies");
select.onchange=function(){
if(select.value=="2"){
document.getElementById("secondpuppytable").style.display="inline";
}else{
document.getElementById("secondpuppytable").style.display="none";
}
}
<p>Please select number of puppies:</p>
<p>
<select id="selectnopuppies">
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<form id="form1" name="form1" method="POST" action="<%=MM_editAction%>">
<p>Puppy 1:</p>
<p> </p>
<table width="330" border="0">
<tr>
<td>Name:</td>
<td>
<input type="text" name="PuppyName1" />
</td>
</tr>
<tr>
<td>Colour:</td>
<td>
<input type="text" name="PuppyColour1" />
</td>
</tr>
<tr>
<td>Sex:</td>
<td>
<select name="PuppySex1" id="PuppySex1">
<option value=" "></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
<tr>
<td>Microchip/DNA Profile:</td>
<td>
<select name="PuppyMicrochip1" id="PuppyMicrochip1">
<option value="No">No</option>
<option value="Microchip">Microchip</option>
<option value="DNA Profile">DNA Profile</option>
</select>
</td>
</tr>
<tr>
<td>
<p>Microchip/DNA Number:</p>
</td>
<td> </td>
</tr>
</table>
<div id="secondpuppytable" style="display:none;">
<p>Puppy 2:</p>
<table width="330" border="0">
<tr>
<td>Name:</td>
<td>
<input type="text" name="PuppyName2" />
</td>
</tr>
<tr>
<td>Colour:</td>
<td>
<input type="text" name="PuppyColour2" />
</td>
</tr>
<tr>
<td>Sex:</td>
<td>
<select name="PuppySex2" id="PuppySex2">
<option value=" "></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
<tr>
<td>Microchip/DNA Profile:</td>
<td>
<select name="PuppyMicrochip2" id="select2">
<option value="No">No</option>
<option value="Microchip">Microchip</option>
<option value="DNA Profile">DNA Profile</option>
</select>
</td>
</tr>
<tr>
<td>
<p>Microchip/DNA Number:</p>
</td>
<td>
<input name="PuppyMicrochipNum2" type="text" id="PuppyMicrochipNum2" />
</td>
</tr>
</table>
</div>
Given that option 1 should show table 1, option 2 show table 1 and 2, and so on.
Try this:
$('#dropdown').change(function(){
var dropdown = $(this);
var tables = $('.tableSelector');
tables.hide();
tables.slice(0,dropdown.val()).show();
});
Working jsfiddle: http://jsfiddle.net/v5TTz/
I have tagged all tables with a css class "tableSelector", then everytime the dropdown changes value, I show the number of tables corresponding to the current value of the dropdownlist. This solution requires the tables to be positioned in the correct order in the DOM.
However, in my ears, this sounds like a case for templates, like jQuery templates or Hoganjs.
I have modified your code... It's working now... try this...
<html>
<head>
<title>Sample</title>
<script type="text/javascript">
function go()
{
var Count = document.getElementById("selCount").options[document.getElementById("selCount").selectedIndex].value;
if(Count==1)
{
document.getElementById("Puppy1").style.display = '';
document.getElementById("Puppy2").style.display = 'none';
}
if(Count==2)
{
document.getElementById("Puppy1").style.display = '';
document.getElementById("Puppy2").style.display = '';
}
}
</script>
</head>
<body>
<p>Please select number of puppies:</p>
<p>
<select onchange = "go()" id="selCount">
<option value="1">1</option>
<option value="2">2</option>
</select>
</p>
<form id="form1" name="form1" method="POST" action="<%=MM_editAction%>">
<p>Puppy 1: </p>
<p> </p>
<table width="330" border="0" id="Puppy1">
<tr>
<td>Name: </td>
<td><input type="text" name="PuppyName1" /></td>
</tr>
<tr>
<td>Colour: </td>
<td><input type="text" name="PuppyColour1" /></td>
</tr>
<tr>
<td>Sex:</td>
<td><select name="PuppySex1" id="PuppySex1">
<option value=" "> </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
<tr>
<td>Microchip/DNA Profile:</td>
<td><select name="PuppyMicrochip1" id="PuppyMicrochip1">
<option value="No">No</option>
<option value="Microchip">Microchip</option>
<option value="DNA Profile">DNA Profile</option>
</select></td>
</tr>
<tr>
<td><p>Microchip/DNA Number:</p></td>
<td> </td>
</tr>
</table>
<p>Puppy 2:</p>
<table width="330" border="0" id="Puppy2">
<tr>
<td>Name: </td>
<td><input type="text" name="PuppyName2" /></td>
</tr>
<tr>
<td>Colour: </td>
<td><input type="text" name="PuppyColour2" /></td>
</tr>
<tr>
<td>Sex:</td>
<td><select name="PuppySex2" id="PuppySex2">
<option value=" "> </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
<tr>
<td>Microchip/DNA Profile:</td>
<td><select name="PuppyMicrochip2" id="select2">
<option value="No">No</option>
<option value="Microchip">Microchip</option>
<option value="DNA Profile">DNA Profile</option>
</select></td>
</tr>
<tr>
<td><p>Microchip/DNA Number:</p></td>
<td><input name="PuppyMicrochipNum2" type="text" id="PuppyMicrochipNum2" /></td>
</tr>
</table>
</body>
</html>
You can do something like this:
When you select 1st option, it will show you firSt table and for 2nd it will show you both tables.
$("table").hide();
$("select option").change(function(){
Val = $(this).val();
If(Val ==1) {
$("table")[0].show();
$("table")[1].hide();
} else {
$("table").show();
}
});
A little bit of javascript:
<script>
function showtable(o){
if(o.value==2){document.getElementById('table2').style.display='block';}
else{document.getElementById('table2').style.display='none';}
}
</script>
<p>Please select number of puppies:</p>
<p>
<select onchange="showtable(this)">
<option selected value="1">1</option>
<option value="2">2</option>
</select>
<table id='table2' width="330" border="0" style="display:none">
HTML Changes
<select onchange="showForm(this.options[this.selectedIndex]);">
Why it's needed ?
For listening the select box value change .
<table width="330" border="0" class="puppy">
Added class=puppy attribute for readability . For hiding table element will be generic and error prone.
Javascript Changes
function showForm(option){
//option user has selected.
var val = parseInt(option.value);
//form elements in a document.
var forms = document.getElementsByClassName("puppy");
for(var i=0,total=forms.length;i<total;i++){
var form = forms[i];
var display = form.style.display;
if(i<val && display == "none"){
form.style.display = "block";
}else if(i >= val && display != "none"){
form.style.display = "none";
}
}
}
Live Demo # http://jsfiddle.net/A3eFz/31/
Please try with following example, hope this helps.
<label for ="amount">Please select number of parameter</label><br/>
<select name="amount" id="amount" title="amount">
<option value="00">0</option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
<option value="06">6</option>
<option value="07">7</option>
<option value="08">8</option>
<option value="09">9</option>
<option value="10">10</option>
</select>
<form name = "AddQuery" id = "AddQuery" method = "POST" action = "submit.php">
<div id="groups">
</div>
<div id="other">
</div>
<input type="submit" value="Submit">
</form>
<script type="text/javascript" src="js/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#amount').change(function(){
$('#groups').empty();
var group = '';
var number = parseInt($(this).val());
for(var i=0;i<number;i++){
group += '<div id="group">' +
'<label for="name['+i+']">Name'+i+'</label> '+
'<input name="name'+i+'" type="text" id="name'+i+'" value="">' +
'<label for="type['+i+']">Type'+i+'</label> '+
'<input name="type'+i+'" type="text" id="type'+i+'" value="">' +
'</div>';
}
$('#groups').append(group);
$('#other').empty();
var other = '';
other +='<div id="other"><input type="hidden" name="n" id ="n" value="'+number+'"/></div>';
$('#other').append(other);
});
});
</script>

Categories

Resources