troubleshooting Question

Getting value of each textarea, If many has same name

Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates asked on
JavaScript
4 Comments2 Solutions361 ViewsLast Modified:
Hi,

I have to some bit in javascript, so I was creating sample code

My problem is:

I have many textfields, textareas of same name in my html file
for example
<textarea cols="50" rows="4" onchange="copychanged=true;" style="width: 400px" name="question41">9 nights hotels</textarea><br />
<textarea cols="50" rows="4" onchange="copychanged=true;" style="width: 400px" name="question41"></textarea><br />
<textarea cols="50" rows="4" onchange="copychanged=true;" style="width: 400px" name="question41"></textarea><br />

How can I fetch value of each textarea with name question41

Basically these are in different table tr. If the value is blank then I wanted to add css style to the tr

var textFieldValue = document.getElementById(textFieldName).value;

alert(textFieldValue)
if(textFieldValue=="")
{
      alert("inside");
      tr.className = "yellow";
}


You can see this in my test code

Please guide

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
tr.yellow td {
	border-top: 1px solid #FB7A31;
	border-bottom: 1px solid #FB7A31;
	background: #FFC;
	}

</style>
<script type="text/javascript">

function test()
{
    var maxRowCount = document.getElementById("maxRowCount").value;
    alert(maxRowCount);

	for (i=1;i<=maxRowCount;i++)
	{
		var trIdName = "question_"+document.getElementById("questionID").value+"_row_"+i;
		var tr = document.getElementById(trIdName);

		if(tr.style.display=='none')
		{

		}else
		{
			var textFieldName = "question"+document.getElementById("questionID").value;
			var textFieldValue = document.getElementById(textFieldName).value;

			alert(textFieldValue)
			if(textFieldValue=="")
			{
				alert("inside");
				tr.className = "yellow";
			}
		}
	}
}

</script>
</head>
<body>
<form>
<input name="questionID" type="hidden" id="questionID" value="41" />
<input name="maxRowCount" type="text" id="maxRowCount" value="3" />

<table width="100%">
<tr>
<td style="width: 608px" valign="top">
	<table width="100%" border="0" cellpadding="3" cellspacing="0">

	<tr id="question_41_row_1">
	    <td align="left">
		<textarea cols="50" rows="4" onchange="copychanged=true;" style="width: 400px" name="question41">9 nights hotels</textarea><br />
	    </td>

	</tr>
	 <tr id="question_41_row_2">
	    <td align="left">
		<textarea cols="50" rows="4" onchange="copychanged=true;" style="width: 400px" name="question41"></textarea><br />
	    </td>

	</tr>

	<tr id="question_41_row_3" style="display: none">
	     <td align="left">
		<textarea cols="50" rows="4" onchange="copychanged=true;" style="width: 400px" name="question41"></textarea><br />
	     </td>

	 </tr>

	 </table>

</td>
</tr>
</table>

<input type="button" onclick="test()" value="Change CSS">
</form>
</body>
</html>
ASKER CERTIFIED SOLUTION
Kin Fat SZE

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros