Jump to content

How do I get a value from one drop down list into a field in a javascript table?


shabbaranks

Recommended Posts

Hi,

 

I've been google'ing until Im blue in the face and think I have found a solution as to what I want to do, but I cant get how I achieve it. Would I be correct in thinking to get the selected value from below

 

<select name="Activity">
<?php
$sql = "SELECT Status FROM Activity";
$rs = mysql_query($sql) or trigger_error(mysql_error() . " unable to run query.");
echo "<option></option>";
while($row = mysql_fetch_assoc($rs))
{
  echo "<option value=\"".$row['Activity']."\">".$row['Status']."</option>\n  ";
}
?>
</select>

 

The array I need is "Activity"?

 

Do I then use javascript getelementbyid? This being the 'Activity' field how do I then push this to the Activity location in the below table? If Im doing this completely wrong would someone be so kind as to point me in the right direction please?

 

<button onclick='addRow();'>Add to Table</button>
<script type='text/javascript'>
function addRow(){
	//get a reference to the table tbody
	var myTable = document.getElementById('myTable').getElementsByTagName('TBODY')[0];
	//create the table row
	var tr = document.createElement('TR');
	//create the 4 column cells
	var td1 = document.createElement('TD');
	 	td1.innerHTML = '<input class="name" />';
	var td2 = document.createElement('TD');
		td2.innerHTML = '<input class="date" />';
	var td3 = document.createElement('TD');
		td3.innerHTML = '<input class="name" />';
	var td4 = document.createElement('TD');
		td4.innerHTML = '<input class="hours" />';
	var td5 = document.createElement('TD');
		td5.innerHTML = '<input class="name" />';
	var td6 = document.createElement('TD');
		td6.innerHTML = '<input class="name" />';
	var td7 = document.createElement('TD');
		td7.innerHTML = '<input class="description" />';	
	//add the column cells to the row
	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);
	tr.appendChild(td4);
	tr.appendChild(td5);
	tr.appendChild(td6);
	tr.appendChild(td7);

	//append the row to the table
	myTable.appendChild(tr);
}
</script>
<table id='myTable'>
<thead>
	<tr>
		<td>Name</td>
		<td>Date</td>
		<td>Project</td>
		<td>Hours</td>
		<td>Department</td>
		<td>Activity</td>
		<td>Description</td>
	</tr>
</thead>
<tbody>
</tbody>
</table>

 

Thanks

Link to comment
Share on other sites

getElemebtById means you're accessing an element by it's id tag, if you don't have one, it won't work...

 

if you simple add an id tag to your select, so instead if this:

<select name="Activity">

you'll have something like this:

<select name="Activity" id="myActivityCombo">

 

then you can easily grab the selected value with something like:

 

var selectedValue = document.getElementById("myActivityCombo").value;

 

hope this helps

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.