How to parse the inner html of a data cell using Java Script.

I need to parse the innerHtml of the data located in each <td> cell using Java Script. Namely I need to do the following.
- Access each row in a table.
- In each row found, access each data element <td> that has an "<input" tag in it.
- If an input control is found, access the id of the input control, and the type of the control
- I must use strictly Java script and no JQuery.

So for the example below, for rows cbx_Row_1,cbx_Row2,cbx_Row_3 I need the following information. I assume the data will be parsed in a loop.

first loop pass : rpt_ctl03_altSelectBoxFirst,checkbox
second loop pass : rpt_ctl03_altSelectBoxSecond,checkbox
third loop pass : rpt_ctl03_altSelectBoxThird,checkbox



<tr Id="cbxRow_1"> 
   <td>
     <span name="123">
       <input  id="rpt_ctl03_altSelectBoxFirst" type="checkbox" name = "rpt$ctl03$altSelectBox" checked="checked">
     </span>
   </td>
 </tr>
  
   <tr Id="cbxRow_2"> 
   <td>
     <span name="123">
       <input  id="rpt_ctl03_altSelectBoxSecond" type="checkbox" name = "rpt$ctl03$altSelectBox" checked="checked">
     </span>
   </td>
 </tr>
  
   <tr Id="cbxRow_3"> 
   <td>
     <span name="123">
       <input  id="rpt_ctl03_altSelectBoxThird" type="checkbox" name = "rpt$ctl03$altSelectBox" checked="checked">
     </span>
   </td>

Open in new window

I know how to iterate through each row, however I don't know how to get the <input> controls and their ids and the control type. Can someone help me out?
Below is the code I have so far. If I search the innerHtml, I get a numeric value of either -1 or 1 to indicate if the value was found. That is useless to me. I need the actual control
values, I noted above.

var table = document.getElementById("PayTable");
   var r = 0;
   while(row=table.rows[r])
   {

	for(var c=0; m = table.rows[r].cells.length; c < m; c++)
        {
		var xx = table.rows[r].cells[c].innerHTML;

        }



   }

Open in new window

LVL 2
brgdotnetcontractorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
Try this. It will show a message box for each INPUT inside the table.
<script language="javascript">
function getInputs() {
	var tbl = document.getElementById("PayTable");
	var inputs = tbl.getElementsByTagName("input");
	for (var idx = 0; idx < inputs.length; idx++) {
		var input = inputs[idx];
		alert(input.id + " = " + input.value);
	}
}
</script>

Open in new window

0
brgdotnetcontractorAuthor Commented:
I can not use the getElementsByTagName approach.


It would be nice however there is a specific reason I can't use it. Instead I need to parse through each row at a time and search for the input tag as I noted in my post. So that was why I wanted to take the approach of searching the innerHtml.
0
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
Then, you will have a lot of code! Here you go:
function getCellHtml() {
    var tbl = document.getElementById("PayTable");
    for (var rIdx = 0; rIdx < tbl.rows.length; rIdx++) {
        var row = tbl.rows[rIdx];
        for (var cIdx = 0; cIdx < row.cells.length; cIdx++) {
            var cell = row.cells[cIdx];
            for (var idx = 0; idx < cell.childNodes.length; idx++) {
                var child = cell.childNodes[idx];
                if (child.nodeName != null && child.nodeName != undefined && 
                    child.nodeName.toUpperCase() == "SPAN") {
                    for (var x = 0; x < child.childNodes.length; x++) {
                        var ctrl = child.childNodes[x];
                        if (ctrl.nodeName != null && ctrl.nodeName != undefined && 
                            ctrl.nodeName.toUpperCase() == "INPUT") {
                            alert(ctrl.id + " = " + ctrl.value);
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
> I must use strictly Java script and no JQuery.

Is this for school? or live project?  What you are asking to do is perfect for jquery and can be done in a few lines that is very easy to read and understand.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
0
brgdotnetcontractorAuthor Commented:
Hi Scott, the question is similar but not the same. I need to access individual elements. Thanks.
0
brgdotnetcontractorAuthor Commented:
Thanks gentlemen.
0
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
Thanks for considering my answer
0
brgdotnetcontractorAuthor Commented:
Your welcome, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.