LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-28-2015, 12:53 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Rep: Reputation: 52
how can I reset all elements to "display:none;"


I need to reset the display of all elements (hundreds) to "display:none;" before executing the javascript routine below so that only one element is visible. How can I do that? Javascript? Or CSS? I only have basic knowledge of both. Any hint most welcome.

Code:
<script type="text/javascript">
function toggle_visibility(id) {
 var e = document.getElementById(id);
  if(e.style.display == 'none')
   e.style.display = 'block';
  else
   e.style.display = 'none';
}
</script>
note: '0001' in the lines below is an incremental value given by script and rising to '0342' for example, it can be changed to a strict number ("1" or "2" or "23" or "342") to suit a loop if necessary and this is an example of the elements:

Code:
Some explanations.<a href="#0001" onclick="toggle_visibility('0001');">toggle_this</a>
<span id="0001" style="display:none;">Some instructions: 
<textarea name="0001" rows="1" cols="80"></textarea>
<input type="submit" value="Report"><input type="submit" value="Submit">
</span>
Thank you for your help.
 
Old 11-28-2015, 05:15 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by rblampain View Post
I need to reset the display of all elements (hundreds) to "display:none;"
for literally all elements, with css:
Code:
* { display: none !important; }
 
1 members found this post helpful.
Old 11-29-2015, 09:33 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Also, every JavaScript library out there has the ability to "select" elements by some criteria, to loop through the selected list, and "hide()" them. It is usually best to leverage one of those libraries, and to use their higher-level routines (such as "hide") instead of direct CSS manipulations.
 
1 members found this post helpful.
Old 11-30-2015, 05:16 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Well javascript has a for loop and you may like to use it

for(i=1;i<343;i++) {
j=""+i ;
if (i<100) j="0"+j ;
if (i<10) j="00"+i ;
document.write("Some explanations.<a href=\"#"+ j + "\"
.....
and so on on
and ending with
.....
</span>
") ; Closing the document write
}

Last edited by AnanthaP; 12-07-2015 at 06:44 AM.
 
1 members found this post helpful.
Old 11-30-2015, 02:24 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I'd do it with two classes: one to specify that an element is hideable and one to specify that it's hidden.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.hidden {
    display: None;
}
</style>
</head>
<body>
<p class="canhide">This is paragraph 1</p>
<p class="canhide">This is paragraph 2</p>
<button>Hide Text</button>
<script>
document.querySelector("button").addEventListener("click", function () {
    [].forEach(document.querySelectorAll("canhide"), function (e) {
        e.classList.add("hidden");
    });
});
</script>
</body>
</html>
In the markup I add the "canhide" class to each element that would need to be hidden. Then in the event handler I select each element with that class and add to it the "hidden" class. Elements with the "hidden" class have their display set to None.

It's very unlikely that you'll actually have to add the "canhide" class to "hundreds" of elements. You can add it to container elements such as divs.

Last edited by dugan; 12-02-2015 at 11:14 PM.
 
1 members found this post helpful.
Old 12-05-2015, 07:59 AM   #6
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 11
Posts: 1,288

Original Poster
Rep: Reputation: 52
This modification works in a test of 3 elements.
<script type="text/javascript">
function toggle_visibility(id) {


for(i=1;i<4;i++) {
elemnt = document.getElementById(i);
elemnt.style.display = "none";
}


var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}
</script>
 
Old 12-05-2015, 11:23 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
If you're going to apply an "id" to more than one element, then make it a class and and not an id. Then get them all at once with querySelectorForAll or getElementsByClassName.

An id is supposed to be unique. An id isn't. That's why the DOM APIs to select ids only return one element. And also why it's getElementById and getElementsByClassName.

Last edited by dugan; 12-05-2015 at 11:26 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." davcefai Debian 1 02-15-2014 03:11 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
"clear" and "reset" bash commands broken AviJacobson Linux - Software 1 06-29-2006 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration