Solved

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

Posted on 2016-07-14
9
39 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

747 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