Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to access hidden field values into function
in javascript

ib below code I want to Access Values of hidden filed the row on which i clicked I want to access that values of that field (i.e stored in hidden field ) in the function

Hidden field is like


var iptext='<label>First Name</label>'+'<input type="hidden" name="FirstName'+i+'" value="FirstName'+i+'">';


for each cell created one hidden field

What I have tried:

<!DOCTYPE html>
<html>
<head>
<style>

table {
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid silver;
}

tr:nth-child(even) {
    background-color: #C0C0C0;
}
tr:nth-child(odd) {
    background-color: #808080;
} 

td, th {
  border-bottom:1px solid #eee;
  background: #ddd;
  color: #000;
  padding: 10px 25px;
}
th {
  height: 0;
  line-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  color: transparent;
  border: none;
  white-space: nowrap;
}
th div{
  position: absolute;
  background: transparent;
  color: #fff;
  padding: 9px 25px;
  top: 0;
  margin-left: -25px;
  line-height: normal;
  border-left: 1px solid #800;
}
th:first-child div{
  border: none;
}


</style>
</head>
<body onload="myFunction()">
//<body onload="myFunction(); alternate();">
<div>
  

<div style="overflow:scroll;height:500px;width:100%;overflow:auto">

<table id=tableData>
</table>
</div>
</div>
<p id="demo"></p>
<script>	
		
	function myFunction()
	{
		var n = prompt("", "");
		n = parseInt(n);
		var table;			
		document.getElementById("demo").innerHTML = n;			
		if (n > 0)
		{
        			
			table=document.createElement("TABLE");		
			
			for(i=0;i<n;i++)
			{
				if(i==0)
				{
				var header = table.createTHead();
					// Create an empty <tr> element and add it to the first position of <thead>:
					var row = header.insertRow(0);     	
					// Insert a new cell (<td>) at the first position of the "new" <tr> element:
					var cell = row.insertCell(0);
					// Add some bold text in the new cell:
					cell.innerHTML = "Id"; 

					var cell = row.insertCell(1);
					// Add some bold text in the new cell:
					cell.innerHTML = "FirstName"; 

					var cell = row.insertCell(2);
					// Add some bold text in the new cell:
					cell.innerHTML = "LastName"; 

					var cell = row.insertCell(3);
					// Add some bold text in the new cell:
					cell.innerHTML = "MobileNo"; 

					var cell = row.insertCell(4);
					// Add some bold text in the new cell:
					cell.innerHTML = "Email Id"; 	
				
				}

				else
				{
				
				var row = document.createElement('tr');
				row.id = i;
			    row = table.insertRow(-1);
				
				row.setAttribute("onclick", "RowClick(id)");
	
			//	row.setAttribute("onclick", "RowClick(i)");	


				
				
				
				
				
				//var row = table.insertRow(-1);
				var x = row.insertCell(0);
				x.innerHTML = '100'+i; 
				x.style.width = '50px';
				
				
			
											
				
				var iptext='<label>First Name</label>'+'<input type="hidden" name="FirstName'+i+'" value="FirstName'+i+'">';
				
				var x = row.insertCell(1);
				x.innerHTML = iptext;
				x.style.width = '100px';
				
				
				
				
			
			
			//var input = document.createElement("input");

			//input.setAttribute("type", "hidden");

			//input.setAttribute("name", "name_you_want");

			//input.setAttribute("value", "value_you_want");

			//append to form element that you want .
			//document.getElementById("chells").appendChild(input);
				

				var x = row.insertCell(2);
				x.innerHTML = 'Last Name'+i; 
				x.style.width = '100px';				
				
				var x = row.insertCell(3);
				x.innerHTML = '998855442'+i
				x.style.width = '100px';

				var x = row.insertCell(4);
				x.innerHTML = 'Email'+i;
				x.style.width = '100px';				
            	}
				//alert('hi');	
								
			}
			
		}
		var dvTable = document.getElementById("tableData");
    		dvTable.innerHTML = "";
    		dvTable.appendChild(table);		
	}

	function RowClick()
		{
			
			// var s = document.getElementById(i).value;
			 //document.getElementById("demo1").innerHTML = "The value of z is: " + s;
			alert('hi');
			

			//var FN=document.getElementById('FN'+i;).value
			//var FN=document.getElementById('LN'+i).value;
			//var FN=document.getElementById('Last Name'+i;).value;
			//var FN=document.getElementById('MNo'+i).value;
			//var FN=document.getElementById('Email'+i).value;
    			
                    

	}


</script>
</body>
</html>
Posted
Updated 9-Apr-18 22:41pm

From the point of view of JavaScript there is no difference between input element with type=text or type=hidden...
Both can be accessed with getElementById as long as you assign an id to them...
 
Share this answer
 
Quote:
I want to access hidden field values into function

This not a question ! What is the problem you have with this code ?
With HTML/JavaScript, a field is a field and accessing that field does not depend on visibility.
 
Share this answer
 
If you have the row index the easiest way to get the element is to add an id to it

var iptext='<label>First Name</label>'+'<input type="hidden" name="FirstName'+i+'" id="FirstName'+i+'" value="FirstName'+i+'">';


So if you have i = 5 you can get that element using

var LN=document.getElementById('LastName'+i).value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900