r/javaScriptStudyGroup • • Feb 03 '22

Is there a more efficient way to call the same event handler for multiple DOM elements

2 Upvotes

Hello, apologies for the code snippet its not complicated just very repetitive

I included some images of my mock up portfolio page and tried my best to explain what im trying to achieve. Im trying to make an section that displays an image with the projects title in the middle, and 4 buttons. When clicked it will show a paragraph & hide the title, when you move the mouse it will switch back.

(1) My questions are.. is there a more efficient way to implement this rather than having to assign variables to each element and calling onclick/onmouseout event listeners for each variable. If I wanted to add more sections like this it would be very tedious.

(2) At first the paragraph wouldn't show unless unless I press a button twice the first time round. I found that calling the function at least once for each button fixed this, but I don't understand why.

(3) I also found it strange how I could not get this to work unless I passed the function inside an anonymous function instead of directly assigning the function like so:

button1.onclick = togggleParagraph(heading, paragraph1) why is this?

<ul>
  <li id = "challenges1">Challenges</li>
  <li id = "solutions1">Solutions</li>
  <li id = "features1">Features</li>
  <li id = "tech1">Technologies</li>
</ul>

<figure>
  <img id = "project1" src="resources/images/project2.jpg">
  <h2 id = "one">Martial Arts Club</h2>
  <p id = "p1.1">Lorem ipsum ....</p>
  <p id = "p1.2">Lorem ipsom ....</p>
  <p id = "p1.3">Lorem ipsom ....</p>
  <p id = "p1.4">Lorem ipsum ....</p>
</figure>

<script>
/* Assigning elements to variables */

let heading = document.getElementById('one');
let button1 = document.getElementById('challenges1');
let paragraph1 = document.getElementById('p1.1');
let button2 = document.getElementById('solutions1');
let paragraph2 = document.getElementById('p1.2')
let button3 = document.getElementById('features1');
let paragraph3 = document.getElementById('p1.3');
let button4 = document.getElementById('tech1');
let paragraph4 = document.getElementById('p1.4');

/* If the title is showing and paragraph is hidden, 
show the paragraph and hide the title. Otherwise show
the title and hide the paragraph. */

function toggleParagraph(heading, paragraph) {
    if (paragraph.style.display === 'none' && 
    heading.style.display === 'block') {
      heading.style.display = 'none';
      paragraph.style.display = 'block';
    } else {
      heading.style.display = 'block';
      paragraph.style.display = 'none';
    }
}

/* If the paragraph is showing and title is
hidden, show the title and hide the paragraph. */

function hideParagraph(heading, paragraph) {
  if (paragraph.style.display === 'block' &&
  heading.style.display === 'none') {
    paragraph.style.display = 'none';
    heading.style.display = 'block';
  }
}

/* Button requires 2 clicks to toggle paragraph.
One solution was to call the function once before 
assigning it to event listeners */

toggleParagraph(heading, paragraph1);
toggleParagraph(heading, paragraph2);
toggleParagraph(heading, paragraph3);
toggleParagraph(heading, paragraph4);

button1.onclick = function () {
  toggleParagraph(heading, paragraph1)
};
button2.onclick = function () {
  toggleParagraph(heading, paragraph2)
};
button3.onclick = function () {
  toggleParagraph(heading, paragraph3)
};
button4.onclick = function () {
  toggleParagraph(heading, paragraph4)
};

button1.onmouseout = function () {
  hideParagraph(heading, paragraph1)
};
button2.onmouseout = function () {
  hideParagraph(heading, paragraph2)
};
button3.onmouseout = function () {
  hideParagraph(heading, paragraph3)
};
button4.onmouseout = function () {
  hideParagraph(heading, paragraph4)
};
</script>

r/javaScriptStudyGroup • • Jan 21 '22

Create React App Mind Map. Part 3 of 10: Routing

2 Upvotes

r/javaScriptStudyGroup • • Dec 10 '21

String formatting in JavaScript

Thumbnail
tutorialstonight.com
2 Upvotes

r/javaScriptStudyGroup • • Dec 05 '21

Onbeforeunload when window closes but not reloads

2 Upvotes

I’m trying to use onbeforeunload on c# and Js for the user to make sure that he/she have saved whatever they are working on. So, the problem is when the user reload the page the pop up appears even though they didn’t change anything in the page. Also, I want pop up to appear whenever the user is closing the page and has made changes only. Not when it reloads.


r/javaScriptStudyGroup • • Nov 29 '21

Some methods/functions doest work proper in javascript - Help

2 Upvotes

Hello,

Im pretty new with javascript and im following a course.

I use visual code studio and im doing everything like he does but for example i cant possible make ex: arr.lenght to function, but if i copy / paste the same exact thing from the guides code it works this happens especially in loops ex: for (let i = 0; i < arr.lenght), in this case arr.lenght will work just if i copy/paste it.

I can assigb the arr.lenght to another value and then use that one, but not to use it directly where i need it.

Im lost and this bugs me really hard, couldnt find this issue nowhere on the internet and i really tried but i dont know where the problem s coming from... js? Visual code? Me?


r/javaScriptStudyGroup • • Nov 21 '21

Understanding Web Workers | Multi-Threading In JavaScript

Thumbnail
youtu.be
2 Upvotes

r/javaScriptStudyGroup • • Nov 19 '21

How to Make API Calls in JavaScript

Thumbnail
codingtute.com
2 Upvotes

r/javaScriptStudyGroup • • Nov 19 '21

JavaScript quiz to make solid your knowledge and prepare to interview for 30 mins and 40 questions

Thumbnail proghub.io
2 Upvotes

r/javaScriptStudyGroup • • Nov 13 '21

JavaScript: Four Major Differences in var and let

Thumbnail
codetopology.com
2 Upvotes

r/javaScriptStudyGroup • • Nov 10 '21

JavaScript Basics Revisited

Thumbnail
codetopology.com
2 Upvotes

r/javaScriptStudyGroup • • Oct 04 '21

Javascript Stream: The most up to date articles, podcasts, videos, repos about Javascript (Reactjs, Node, Angular, Vue ..etc)

Thumbnail
shoufi-press.tech
2 Upvotes

r/javaScriptStudyGroup • • Sep 20 '21

How to Convert JavaScript Array to Set

Thumbnail
askjavascript.com
2 Upvotes

r/javaScriptStudyGroup • • Sep 16 '21

Explain this function to me

2 Upvotes

If you can explain this code to me in the plainest english possible, I'll be forever grateful.

'''''''''''''''''''''''''''''''''''''''''''''

function addTo(n){

let sum = 0;

for (let i = 0; i <= n; i++) {

sum = sum + i;

}

return sum;

}

'''''''''''''''''''''''''''''''''''''''''''''

so as far as my understanding...

I have created a function to addTo (n).

There is a variable called sum which has a value of 0.

the loop begins at 0, conditional is if the variable is less than or equal to n, add 1 each iteration of the loop.

each time the loop goes around, the sum should equal sum plus 1 (this is where I get confused because actually, it is adding each previous loop onto the previous answer, so 0, 1, 3, 6 <--- I do not understand how or why)

return sum <---- what is this doing, returning the function? I thought that should be return addTo; why sum? ?

Peace and love!


r/javaScriptStudyGroup • • Aug 21 '21

Aula 4 Lógica de programação com Javascript, Funções

Thumbnail
youtube.com
2 Upvotes

r/javaScriptStudyGroup • • Aug 17 '21

Coding vs Scripting | Major Differences You Should Know?

Post image
2 Upvotes

r/javaScriptStudyGroup • • Jul 30 '21

How to Customise Material UI's Default Styling | JavaScript Works

Thumbnail
javascript.works-hub.com
2 Upvotes

r/javaScriptStudyGroup • • Jun 27 '21

okay i guess this is basic on classes but am learning, why is this variable undefined in the bottom code but it's working in the top

2 Upvotes

so i copied the code of a mempory game am just changing what happens after you lose or you win, divs add and remove classes to light or darken the divs, after line 436 on the whole code its my added code, the problem as far as the console is this

this.colores.amarillo.classList.add("light"); is undefined

it points to a specific div, this same code is used lines above to choose color and show sequence, and it works, but when you lose or win it doesnt work, i have one other callback fucntion after you win , this one (ganoJuegoJ is to appear a big div annuncing the win, it show the div bit it doesn't trigger the callbakc function to make it disapear

the whole game is inside a class, maybe it has something to do with binding a specifric function to this, but i dont have that clear at the moment

here is the whole code:

https://github.com/moorooba/simon/blob/master/simonmineclass.html


r/javaScriptStudyGroup • • Jun 10 '21

Useful Books That Java Developers Should Definitely Read

2 Upvotes

Hello folks, If you are a Java developer and wondering what to read in the remaining of 2021, then you have come to the right place. In this article, I am going to share books on Java, Spring, and related technology a Java developer can read.

I have included books for both experienced Java developer who is more inclined to learn about architecture stuff, developing for Cloud, MicroService, Java 9 to Java 14 features, and Spring 5 features and thinking of learning Kotlin to improve their productivity.

Still, I have not forgotten junior and less experienced Java developers or some who are thinking of starting their journey into the Java world in 2021. This means if you have just started learning Java or thinking to learn, those are ideal books to start.


r/javaScriptStudyGroup • • Jun 10 '21

Android Apps To Learn Java Language

2 Upvotes

So, you decided to study Java, even chose a course to take, but here it turns out that you have an extremely busy schedule, and it is very difficult to open a laptop and start learning something. If the situation seems very familiar to you, then the best option for you is to switch to the course, which is presented not only on the desktop, but also in the mobile version.

There are very good platforms on Android that can help you learn Java programming. So, let's talk about these apps.


r/javaScriptStudyGroup • • May 31 '21

Chrome Extension Interval Timer (Time-Boxing)

2 Upvotes

First time touching Chrome Extensions. Let me know if there are any obvious errors!

------------------------------------------

Video: https://youtu.be/jN8c1lD7qyg

GitHub: https://github.com/LexingtonWhalen/ChromeTimer

NOTE: I have yet to add styling. It works fine, but will look prettier within a bit. Keep your eyes peeled!

What is it?

* An interval timer chrome extension.

* Two linked timers (dual timer)

* When one done, the next goes, repeat until no more rounds.

* For example, can set 30 minutes, then when done 15 seconds, repeat 3 times. * In timer-speak: timer01 30 min, timer02 15 sec, rounds = 3.

* Good for timeboxing: https://en.wikipedia.org/wiki/Timeboxing

Ideas for Improvement:

* Clean it up, make it pretty, package it for Chrome extension store!

Features:

* Is a dual timer!

* When one timer done, sound played and next timer starts!

* Keeps going even when popup window closed!

* When all rounds complete, plays song!


r/javaScriptStudyGroup • • May 30 '21

Getting Started with React Hooks

Thumbnail
piyushsinha.tech
2 Upvotes

r/javaScriptStudyGroup • • May 03 '21

ASYNC AWAIT HELP PLEASE

2 Upvotes

Hello please i have an issue i am facing with my project

i have an async function below that reads a file and if the file doesn't exist the catch block handles it. the function works fine

lib.read = async function (dir,file){

try{

await fs.readFile(lib.baseDir+dir+'/'+file+'.json','utf-8')

}

catch(error){

console.log("file doesn't exist) }

}

now i want to create users with each user having a file name so i want to use the function above to check whether the user being created exist's already and when the function triggers the catch block it means the user doesn't exist already and then i can proceed to create the user. Problem is i can't manipulate the to get a response from the catch block that i can use in a conditional to proceed. If i was using a callback i could have returned error and use the error in my conditional but due to callback hell i am not using callback . Please help if you can


r/javaScriptStudyGroup • • Apr 27 '21

JavaScript Study Topic: Practical Usage of Closure in JavaScript

2 Upvotes

Closure is a very important concept that every JavaScript developer should understand. This post will cover some of the practical use of closures.

  1. Private variables and methods

  2. Function factories

Blog: https://www.sudipta-deb.in/2021/04/practical-examples-for-javascript.html

Note - I am sharing all my JavaScript Certification study notes below. Please provide your feedback if you see anything wrong or missing. Appreciate your help.

https://www.sudipta-deb.in/p/salesforce-javascript-developer-i-study.html


r/javaScriptStudyGroup • • Apr 21 '21

JavaScript Topic: Understanding JavaScript Closure

2 Upvotes

JavaScript Topic: Understanding JavaScript Closure

Closure is a very important concept that every JavaScript developer should understand. In the post, I will provide a quick introduction of closure and then, I will be focusing on examples so that it will be easier for you to understand this concept.

https://www.sudipta-deb.in/2021/04/understand-javascript-closure.html


r/javaScriptStudyGroup • • Apr 13 '21

Functional Programming In JavaScript/TypeScript for Beginners

Thumbnail
blog.decipher.dev
2 Upvotes