Jump to content
Stef's Coding Community
Hersarma

functions

Recommended Posts

Hello all.

So i have a question about functions.

Example:

I like to change css style on multiple id's in a function like this.

/ *

function endGame () {
   
     document.getElementById ('easybtn'). style.display = 'none';
     document.getElementById ('mediumbtn'). style.display = 'none';
     document.getElementById ('hardbtn'). style.display = 'none';
     document.getElementById ('randombtn'). style.display = 'none';
     document.getElementById ('textOutput'). style.display = 'none';
     document.getElementById ('hide'). style.display = 'none';

}

* /

But it looks like a lot of repiting.

So i made another function where i used array of id's and use foor loop.

/ *

var ids = ['easybtn', 'mediumbtn', 'hardbtn', 'randombtn', 'textOutput', 'hide']

function endGame () {
     for (var i = 0; and <ids.length; and ++) {
         document.getElementById (ids ). style.display = 'none';
     }

* /
They both work
But what function is bether to use in this situation or is there even a bether way.

Ty.

And sry for my bad english.

Share this post


Link to post
Share on other sites

Hi,

Your second option where you use an array is better, since it is cleaner code. That said, you could also use the element hierarchy in the DOM to target elements. But what would be the point?

Another thing you can do, if possible, is place all those elements into a div and simply toggle the visibility of the div. But I am guessing the elements are in different parts of the page. 

Stef

Share this post


Link to post
Share on other sites

Ty for answer. And yes they are all in one div except one tag. I try to move that tag in another div. Or i can delete that div and add another div in same place whit a function. But any way ty😊

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...