Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Improving the speed of script?

Status
Not open for further replies.

fixthebug2003

Programmer
Oct 20, 2003
294
US
Hi,
I have a very complicated javascript page, that has say 20 rows and may be 30 columns..the thing is the rows and the columns count can change dynamically..I build it using the data comming from the XML.
Now Populating the data is fine...but the problem is , I have these checkboxes for each row and column, which the user can check or uncheck it and there is this One Check box that lets you check/uncheck all the checkboxes in all the rows and columns at one shot.
So when the user checks this "Select All" check box I have to loop through all the checkboxes in the rows and columns and either check or uncheck it...and that becomes very slow..it takes around 5-6 seconds to check/uncheck all the checkboxes...!
Is there a way to increase the speed of execution of the script?

Fixthebug2003
 
Off hand I can't imagine why it is taking so long to execute the script. I have done similar scripts that take no more time than any "normal" scripts. Are you rebuilding from the XML on each check?
 
Post the code you're using to do this now.

--Dave
 
Are you using [tt]eval()[/tt]?

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
This is one problem that a lot of people tackle in a number of different ways. Some are a lot slower than others.

As chessbot hints, anything using a counter and the [tt]eval()[/tt] statement will be fairly slow.

The method I use is to, firstly, make sure I have all my checkboxes wrapped up in a [tt]<div>[/tt] tag for easy access then use the [tt]getElementsByTagName[/tt] function to return a list that I can sift through.
Code:
function do_check_all(strContainerID){
  var objContainer = document.getElementById(strContainerID);
  var allInputs = objContainer.getElementsByTagName("input");
  for(var i = 0; i < allInputs.length; i++){
    var currentInput = allInputs.item(i);
    if(currentInput.type == "checkbox"){
      currentInput.checked = true;
    }
  }
}

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
dwarfthrower,

I never realized you could isolate the form elements you grab to merely those within a particular DIV object. Very cool. Thanks for that!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top