473,378 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Possible to shorten this script, or improve it?

shelzmike
Hey all, I am learning javascript at the moment and am loving the challenges. I spent a lot of time figuring out how to do this particular task, which I succeeded in doing; however, I am interested in determining if it can be done in a cleaner fashion...it seems like I should be able to.

Here is the general gist of what was done.

Have a table like so:
11 12 13
21 22 23
31 32 33
41 42 43

The initial HTML of this table is:
Expand|Select|Wrap|Line Numbers
  1. <table id="users_table" style="border:1px solid #000;border-collapse: collapse;" border='1'>
  2.     <tr class="dataRow">
  3.         <td>11</td>
  4.         <td>12</td>
  5.         <td>13</td>
  6.     </tr>
  7.     <tr class="dataRow">
  8.         <td>21</td>
  9.         <td>22</td>
  10.         <td>23</td>
  11.     </tr>
  12.     <tr id="row3" class="dataRow">
  13.         <td>31</td>
  14.         <td>32</td>
  15.         <td>33</td>
  16.     </tr>
  17.     <tr class="dataRow">
  18.         <td>41</td>
  19.         <td>42</td>
  20.         <td>43</td>
  21.     </tr>
  22. </table>
I have a function that actually adds <span></span> tags to each of the <td>'s,

as an example:
Expand|Select|Wrap|Line Numbers
  1. <tr class="dataRow">
  2.         <td><span>11</span></td>
  3.         <td><span>12</span></td>
  4.         <td><span>13</span></td>
  5.     </tr>
The function in question is one that actually removes the <span> tags and returns the HTML to the original markup (and before anyone wonders why in the heck I want to do this - it is a challenge project for class, so there is no real rhyme or reason).

Now, I was able to do this with a function that essentially captures the childNode that holds the number data and store that in an array.

Then I remove all childNodes of all td tags, then finally append child nodes with the values from the array created above, like so:

Expand|Select|Wrap|Line Numbers
  1. function unHideRawData() {
  2.   var tbl = document.getElementById("users_table");
  3.   var tblRows = tbl.getElementsByTagName("tr");
  4.   var rawData = tbl.getElementsByTagName("td");
  5.  
  6.   var removed = new Array();
  7.   for (r=0; r < rawData.length; r++){
  8.       removed[r] = rawData[r].firstChild.firstChild;
  9.   }
  10.  
  11.   for (x=0; x < rawData.length; x++){
  12.       if (rawData[x].hasChildNodes()) {
  13.           while (rawData[x].childNodes.length >=1) {
  14.             rawData[x].removeChild(rawData[x].firstChild);
  15.         }
  16.       }
  17.   }
  18.  
  19.   for(k=0; k < rawData.length; k++){
  20.       rawData[k].appendChild(removed[k]);
  21.   }
  22.  
  23.   //alert("Raw data is now showing again");
  24. } // end unHideRawData
So this works really well and took me a lot of trying to get here; however, can I make it any smaller, or condensed? i.e, can any steps be combined, etc.?

Thanks!

Mike
Oct 13 '11 #1
3 1291
Dormilich
8,658 Expert Mod 8TB
this is already small. the only optimisation I see is var removed = new Array(); => var removed = [];
Oct 13 '11 #2
gits
5,390 Expert Mod 4TB
one more improvement would be to change things like this:

Expand|Select|Wrap|Line Numbers
  1. for (r=0; r < rawData.length; r++){
to this:

Expand|Select|Wrap|Line Numbers
  1. for (var r = 0, l = rawData.length; r < l; r++) {
this would avoid the evaluation of length in every step.
Oct 13 '11 #3
Thanks for the feedback!
Oct 13 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
2
by: N. Schultheiss | last post by:
I'm new at this and wonder if this is possible. I'm running a Win2k server with FP 2002 Extensions using discussion boards. There is a time delay in the posted content and the way our moderators...
1
by: Treetop | last post by:
I used the following script for a site to stop displaying after a date / time, but it seems huge. Any ideas on how to make it shorter? There are many events in this list. function events() {...
2
by: Francois | last post by:
Hi, This is probably a very easy question...but I just don't know the answer... I want to shorten this script: document.formulier.form_o_stoel1.checked = false;...
4
by: shlomi.schwartz | last post by:
Hi All, Is it possible to open the select (ComboBox) drop down list by script?
1
by: Ennio-Sr | last post by:
Hi all! I'm writing a script that presents the user with a numbered lines menu, each line corresponding to a <case n> which executes a psql command. As the psql-commands are very similar to each...
13
by: Paul | last post by:
Hi I have a .net application that shows the start page for a few seconds and then goes to another start page. I was wondering if it would be possible to put a count on the page to let the user...
5
by: Rick Brandt | last post by:
I've done some Googling on this, but can't find anything definitive looking that isn't ancient. The issue is whether the simple act of viewing an HTML page that contains script or viewing an...
8
by: John | last post by:
Hi, gurus, How can I implement the following feature in C#: Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup & ", group") For Each objMember In objGroup.Members...
5
by: Simon | last post by:
I heard that we could do that by using AJAX. Could anybody share how to do it? Thanks.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.