Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

count row of <tr> then create <select><option> Javascript

Posted on 2009-04-06
5
Medium Priority
?
1,046 Views
Last Modified: 2012-05-06
Hi,

I'm trying to create some javascript to count the number of rows from a table:

<table border="1" id="t1">
<tr id="row1"><td>field 1</td></tr>
<tr id="row2"><td>field 2</td></tr>
<tr id="row3"><td>field 3</td></tr>
</table>

Then from this count create a <select><option> list. Notice the id and values are the same.

<form>
<select name="Append new row before this selected row" id="forAppendNewRow">
<option id="row1">field 1</option>
<option id="row2">field 2</option>
<option id="row3">field 3</option>
</select>
</form>

Thank you,
vkimura
0
Comment
Question by:Victor Kimura
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Expert Comment

by:y0usuf
ID: 24077656
As I see your question, I can suggest a very simplistic answer but you might have to tweak it a little to change that.

First get the the innerHtml of the table and then use regular expression and replace function to replace tr with option tag and just remove td tags and thats it!

if you are using any javascript library like jquery or dojo then it will be very easy to do this
0
 
LVL 15

Accepted Solution

by:
fsze88 earned 1000 total points
ID: 24078081

<table border="1" id="t1">
<tbody>
<tr id="row1"><td>field 1</td></tr>
<tr id="row2"><td>field 2</td></tr>
<tr id="row3"><td>field 99</td></tr>
</tbody>
</table>
 
<form>
<select name="selectionName" id="forAppendNewRow">
<option id="row1">field 1</option>
<option id="row2">field 2</option>
<option id="row3">field 3</option>
</select>
</form>
 
 
<script type="text/javascript">
var i;
var tabletag = document.getElementById("t1");
var theTableBody = tabletag.tBodies[0]
var tableBodychilds = theTableBody.childNodes;
var idArray = new Array();
var fieldArray = new Array();
var tdtag;
var numOfTr;
numOfTr = 0;
for (i=0;i<tableBodychilds.length ; i++){
  if (tableBodychilds[i].nodeName == 'TR'){
    numOfTr++;
//    alert(tableBodychilds[i].attributes['id'].value);
    idArray.push(tableBodychilds[i].attributes['id'].value);
    tdtag = tableBodychilds[i].firstChild;
    fieldArray.push(tdtag.innerHTML);
//    alert(tdtag.innerHTML);
  }
}
 
var selectionObj = document.getElementById("forAppendNewRow");
selectionObj.length=0;
 
for (i=0;i<numOfTr ; i++){
  var newOption = new Option(fieldArray[i], idArray[i]);;
 
  newOption.setAttribute( 'id', idArray[i] );
  selectionObj.options[i] = newOption;
}
 
 
</script>

Open in new window

0
 

Author Comment

by:Victor Kimura
ID: 24078087
Hi y0usuf,

I don't wish to use dojo or jquery right now since I'm trying to learn javascript. Can you show me the code for it?

Thank you,
vkimura
0
 
LVL 4

Expert Comment

by:y0usuf
ID: 24081839
you mean in Javascript or Jquery?
0
 
LVL 4

Assisted Solution

by:y0usuf
y0usuf earned 1000 total points
ID: 24082463
try the following.

// call this in onload 
function tableLoad() {
            var tabletag = document.getElementById("t1").innerHTML;
            var select = tabletag.replace(/tr/gi, "option").replace(/tbody/gi, "select").replace(/<td>|<\/td>/gi, "");
            alert(select);
        }

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

618 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