Solved

asp.net, jquery, javascript select number to hide/show

Posted on 2016-07-14
9
53 Views
Last Modified: 2016-09-03
I have below codes and it works. However,
                     
I have  totalKids = 3 hard codes and now I want to get it from <select> value.
How can I do that?

I use asp.net/c# with javascript.

Thanks,

<div class="form-group input-group">
                                            <span class="input-group-addon"><i class="fa fa-plus"></i></span>                                              
                                                <select onchange="ShowDependentDiv()" required class="form-control" id="totalCountKid" name="totalCountKid">
                                                  <option value="" disabled="disabled" selected="selected" class="disabled">Total Dependent(s)</option>
                                                  <option 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>
                                                  <option value="10">10</option>
                                               </select>
                                        </div>
                                <%
                                int i;
                                int? totalKids;
                                totalKids = 3;
                                Response.Write("<div id='dependentContainer' style='display:none' class='form-group input-group'>");                                                            
                                Response.Write("<table>");
                                for (i = 0; i < totalKids; i++)
                                {
                                %>
                                <tr>
                                <td>
                                <div  class='form-group input-group'><span  class='input-group-addon'><i class='fa fa-user'></i></span>
                                <input   placeholder="First Name" class="form-control"  type="text" id="kidFirstName<%=i%>" name="kidFirstName<%=i%>">
                                </div>
                                </td>
                                <td>
                                <div style="float:left;" class='form-group input-group'><span  class='input-group-addon'><i class='fa fa-user'></i></span>
                                <input   placeholder="Last Name" class="form-control"  type="text" id="kidLastName<%=i%>" name="kidLastName<%=i%>">
                                </div>
                                </td>
                                <td>
                                <div style="float:left;" class='form-group input-group'><span  class='input-group-addon'><i class='fa fa-user'></i></span>
                                <input placeholder="Middle Name" class="form-control"  type="text" id="kidMiddleName<%=i%>" name="kidMiddleName<%=i%>">
                                </div>
                                </td>
                                <td>
                                 <div class="form-group input-group"><span class="input-group-addon"><i class="fa fa-fire"></i></span>
                                 <select required class="form-control" id="primaryTotalDependent<%=i%>" name="primaryTotalDependent<%=i%>">
                                     <option value='' disabled='disabled' selected='selected' class='disabled'>Smoke(Yes/No)</option>
                                     <option value=NO>NO</option><option value=YES>YES</option></select>
                                </div>
                                </td>
                                </tr>
                                <%
                                }
                                Response.Write("</table>");
                                Response.Write("</div>");                            
                                %>
<script>
function ShowDependentDiv() {
                            if (document.getElementById("totalCountKid").value != "0") {
                                document.getElementById("dependentContainer").style.display = 'block';
                             }
                             else {
                                document.getElementById("dependentContainer").style.display = 'none';
                             }

                         }
</script>
0
Comment
Question by:ITsolutionWizard
  • 5
  • 3
9 Comments
 
LVL 12

Expert Comment

by:jitendra patil
ID: 41711740
you can try the below line of code to get the selected value from  select dropdownlist

$( "#totalCountKid option:selected" ).text();    // to get the text

$('#totalCountKid option:selected').val();  // to get the value

HTH
0
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41711807
For pure javascript,
//Get Object of select tag
var e = document.getElementById("totalCountKid");
//Get Value of selected item
var strValue = e.options[e.selectedIndex].value;
//Get Text of selected item
var strText = e.options[e.selectedIndex].text;

Open in new window

For more info please check:
http://www.w3schools.com/jsref/dom_obj_select.asp
0
 

Author Comment

by:ITsolutionWizard
ID: 41711809
It is combined with net your solution does not work to me
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41711863
You  need to modify code and need to make round trip to your asp.net page

Use below code instead of <select>
<asp:DropDownList ID="totalCountKid" runat="server"  onselectedindexchanged="itemSelected">
<!-- your options -->
</asp:DropDownList> 

<%
protected void itemSelected(object sender, EventArgs e)
{
    totalKids = logList.SelectedIndex].Value;
}
%>

Open in new window

0
 

Author Comment

by:ITsolutionWizard
ID: 41712958
I use html dropdown not asp net dropdown
0
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41713781
then, you need to change your below asp.net code into javascript
    <%
                                int i;
                                int? totalKids;
                                totalKids = 3;
                                Response.Write("<div id='dependentContainer' style='display:none' class='form-group input-group'>");                                                            
                                Response.Write("<table>");
                                for (i = 0; i < totalKids; i++)
                                {
                                %>
                                <tr>
                                <td>
                                <div  class='form-group input-group'><span  class='input-group-addon'><i class='fa fa-user'></i></span>
                                <input   placeholder="First Name" class="form-control"  type="text" id="kidFirstName<%=i%>" name="kidFirstName<%=i%>">
                                </div>
                                </td>
                                <td>
                                <div style="float:left;" class='form-group input-group'><span  class='input-group-addon'><i class='fa fa-user'></i></span>
                                <input   placeholder="Last Name" class="form-control"  type="text" id="kidLastName<%=i%>" name="kidLastName<%=i%>">
                                </div>
                                </td>
                                <td>
                                <div style="float:left;" class='form-group input-group'><span  class='input-group-addon'><i class='fa fa-user'></i></span>
                                <input placeholder="Middle Name" class="form-control"  type="text" id="kidMiddleName<%=i%>" name="kidMiddleName<%=i%>">
                                </div>
                                </td>
                                <td>
                                 <div class="form-group input-group"><span class="input-group-addon"><i class="fa fa-fire"></i></span>
                                 <select required class="form-control" id="primaryTotalDependent<%=i%>" name="primaryTotalDependent<%=i%>">
                                     <option value='' disabled='disabled' selected='selected' class='disabled'>Smoke(Yes/No)</option>
                                     <option value=NO>NO</option><option value=YES>YES</option></select>
                                </div>
                                </td>
                                </tr>
                                <%
                                }
                                Response.Write("</table>");
                                Response.Write("</div>");                            
                                %>

Open in new window

Use below code:
- HTML Code, simply put below div container
<div id='dependentContainer'></div>
<script>
function ShowDependentDiv() {
	var totalKid = document.getElementById("totalCountKid").value;
	if (totalKid>0) {
		createChildElements(totalKid);
		document.getElementById("dependentContainer").style.display = 'block';
	}else{
		createChildElements(0);
		document.getElementById("dependentContainer").style.display = 'none';
	}
}
	
createChildElements = function (n){
	var htmlStr='';
	for(int i=0;i<n;i++)
	{
		htmlStr+= createElement(i+1);
	}
	var divParent = document.getElementById('dependentContainer');
	divParent.innerHTML = '<table>'+htmlStr+'</'+'table>';
}

createElement = function(i){
var html='<tr>';
html+='<td>'
+'<div class="form-group input-group"><span  class="input-group-addon"><i class="fa fa-user"></i></span>
<input placeholder="First Name" class="form-control"  type="text" 
	id="kidFirstName"'+i+'" name="kidFirstName"'+i+'">
</div>'
+'</'+'td>';

//------------------------------
html+='<td>'
+' your html code here... '
+'</'+'td>';
//------------------------------

html+='</tr>';
return html;
}
</script>

Open in new window

0
 

Author Comment

by:ITsolutionWizard
ID: 41715828
Please show me the working codes
0
 
LVL 8

Accepted Solution

by:
Prakash Samariya earned 500 total points (awarded by participants)
ID: 41716387
Please find the working code
Note: I have added input for "FirstName" only. You need to do the same for the rest of columns!
<html>
<body>
<select onchange="ShowDependentDiv()" required class="form-control" id="totalCountKid" name="totalCountKid">
  <option value="" disabled="disabled" selected="selected" class="disabled">Total Dependent(s)</option>
  <option 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>
  <option value="10">10</option>
</select>

<div id='dependentContainer' style="display:none;"></div>
<script>
function ShowDependentDiv() { //debugger;
	var totalKid = document.getElementById("totalCountKid").value;
	if (totalKid>0) {
		createChildElements(totalKid);
		document.getElementById("dependentContainer").style.display = 'block';
	}else{
		createChildElements(0);
		document.getElementById("dependentContainer").style.display = 'none';
	}
}
	
function createChildElements(n){
	var htmlStr='';
	for(i=0;i<n;i++)
	{
		htmlStr+= createElement(i+1);
	}
	var divParent = document.getElementById('dependentContainer');
	divParent.innerHTML = '<table>'+htmlStr+'</'+'table>';
}

function createElement(i){
var html='<tr>';
html+='<td>'
+'<div class="form-group input-group">'
+'<span  class="input-group-addon"><i class="fa fa-user"></i></span>'
+'<input placeholder="First Name" class="form-control"  type="text" id="kidFirstName"'+i+'" name="kidFirstName"'+i+'">'
+'</div>'
+'</'+'td>';

//--YOUR CODE TO ADD MORE COLUMNS ---------
//html+='<td>'
//+' your html code here... '
//+'</'+'td>';

//...

//html+='<td>'
//+' your html code here... '
//+'</'+'td>';
//-----------------------------------------

html+='</tr>';
return html;
}
</script>
</body>
</html>

Open in new window

Working  code snapshot below
snapshot of working sample
0
 
LVL 8

Expert Comment

by:Prakash Samariya
ID: 41782740
Working Solution has been provided!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now