Solved

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

Posted on 2016-07-14
9
45 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to dynamically set the form action using jQuery.

914 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

18 Experts available now in Live!

Get 1:1 Help Now