473,387 Members | 1,790 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,387 software developers and data experts.

Can this be written in a more efficient way

Hi

I've managed to figure out how to write some code which acts to
show/hide sections of a web page. The code is below:

function showreimburse(){
document.getElementById("reimburse").style.display ="block";
}
function hidereimburse(){
document.getElementById("reimburse").style.display ="none";
}
function showreimburselength(){
document.getElementById("reimburselength").style.d isplay="block";
}
function hidereimburselength(){
document.getElementById("reimburselength").style.d isplay="none";
}
function showq2b(){
document.getElementById("q2b").style.display="bloc k";
}
function hideq2b(){
document.getElementById("q2b").style.display="none ";
}
function showwhere(){
document.getElementById("where").style.display="bl ock";
}
function hidewhere(){
document.getElementById("where").style.display="no ne";
}
function showquestion3a(){
document.getElementById("question3a").style.displa y="block";
}
function hidequestion3a(){
document.getElementById("question3a").style.displa y="none";
}

which I trigger in the web page with this sort of thing. All fine so
far. However is it possible to write the javascript in a more
effiecient manner.

Thanks for reading.

Alex

Jan 18 '06 #1
4 1270
What about this?

function hideSomething (what, whatTo) {
var obj = document.getElementById(what);
if(obj === null) return;
obj.style.display = whatTo;
}

Then call...

hideSomething("question3a", "none");

Jan 18 '06 #2
Ronaldo Junior wrote on 18 jan 2006 in comp.lang.javascript:
What about this?

function hideSomething (what, whatTo) {
var obj = document.getElementById(what);
if(obj === null) return;
obj.style.display = whatTo;
}

Then call...

hideSomething("question3a", "none");


That is less efficient tha the loop solutions,
where the whole point of testing was ending
the series of available divs.

And your function can be simplified:

function hideSomething(whatID, whatTo) {
var obj = document.getElementById(whatID);
if (obj) obj.style.display = whatTo;
}

Or even more so:

function hideSomething(whatID, whatTo) {
if (obj = document.getElementById(whatID))
obj.style.display = whatTo;
}


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 18 '06 #3
On 2006-01-18, alex_NOSPAM_BILLEREY@HOTMAILDOTCOM <al***********@hotmail.com> wrote:
Hi

I've managed to figure out how to write some code which acts to
show/hide sections of a web page. The code is below:

function showreimburse(){
document.getElementById("reimburse").style.display ="block";
} (10+ similar skipped)

which I trigger in the web page with this sort of thing. All fine so
far. However is it possible to write the javascript in a more
effiecient manner.

function show(a){
document.getElementById(a).style.display="block";
}
function hide(a){
document.getElementById(a).style.display="none";
}
then when you need them do

show('question3a');

or

hide('where');

etc.

show might be betted done as

function show(a){
document.getElementById(a).style.display="";
}

in-case you want to (un)hide inline elements...

Bye.
Jasen
Jan 19 '06 #4
JRS: In article <11**********************@g44g2000cwa.googlegroups .com>
, dated Wed, 18 Jan 2006 09:42:03 remote, seen in
news:comp.lang.javascript, alex_NOSPAM_BILLEREY@HOTMAILDOTCOM
<al***********@hotmail.com> posted :
[similar] ... function showquestion3a(){
document.getElementById("question3a").style.displ ay="block";
}
function hidequestion3a(){
document.getElementById("question3a").style.displ ay="none";
}

which I trigger in the web page with this sort of thing. All fine so
far. However is it possible to write the javascript in a more
effiecient manner.


YSCIB. Yes.

Evidently (at least typically) the argument to
document.getElementById("question3a")
is a constant; and the call is made at least twice and maybe more often.
Now getElementById is a searching operation, and takes time, especially
on a large page.

You could write those functions as

var gIDQ3A = document.getElementById("question3a")

function showquestion3a() { gIDQ3A.style.display="block" }
function hidequestion3a() { gIDQ3A.style.display="none" }

and perhaps move .style as well. There ought to be a way of getting a
"pointer" to gIDQ3A.style.display itself; but I don't know of one.

Never perform a given lookup many times if it can easily enough be
avoided.

And now the content of the functions is sufficiently simple that it can
reasonably be written in-line, saving the overhead of a function call.
OTOH, the time taken by simple code that affects the appearance of the
screen will be dominated by screen-handling.

OTTH, the screen should be rendered only once for each set of calls.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sandeep | last post by:
Hi i am new to this group and to c++ also though i have the knowledge of "c" and now want to learn c++ and data structure using c/c++ . so could nebody please suggest me some...
2
by: Vance M. Allen | last post by:
Greetings, I am establishing a database for the purpose of logging access to my secure webserver and am wanting to make the database as efficient as I can because it will be doing a lot of work...
19
by: petantik | last post by:
Where can I find a good resource, on the web, that will give me a good comprehensive idea of how to write a compiler in C for C or maybe another language. http://petantik.blogsome.com - A...
2
by: Alison | last post by:
Hi, all What is the best way to use functions written in C in VB .Net without compromsing much of the speed of computation I am desiging a new user interface in VB .Net, but the original source...
3
by: Brian Wotherspoon | last post by:
I have a table with data that is refreshed regularly but I still need to store the old data. I have created a seperate table with a foreign key to the table and the date on which it was replaced. ...
4
by: Karlo Lozovina | last post by:
Are there any forum or bulletin board systems written entirely in Python? I got sick of PhpBB, mostly because I can't tweak and fiddle with the code, since I really don't like PHP and don't know it...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
3
by: =?Utf-8?B?Um9nZWxpbw==?= | last post by:
hey, got a method here (that I took from somewhere) that gets a list of all the files in a folder., /// <summary> /// Method that gets all the files in a directory and returns an arraylist of...
25
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.