Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

button click

const thing = document.querySelector("#get-residents")


const cons = () => {
    console.log('click')
}

thing.addEventListener('click', cons)

/* this is what the query selector needs to operate 
    <button id="get-residents">get residents</button>
    
    <script src="main.js"></script>
    <script src="./node_modules/axios/dist/axios.min.js"></script>
*/
Comment

button click event

// Cache out buttons container, and all of the panels
const buttons = document.querySelector('.buttons');
const panels = document.querySelectorAll('.panel');

// Add an event listener to the buttons container
buttons.addEventListener('click', handleClick);

// When a child element of `buttons` is clicked
function handleClick(e) {
 
  // Check to see if its a button
  if (e.target.matches('button')) {

    // For every element in the `panels` node list use `classList`
    // to remove the show class
    panels.forEach(panel => panel.classList.remove('show'));

    // "Destructure" the `id` from the button's data set
    const { id } = e.target.dataset;

    // Create a selector that will match the corresponding
    // panel with that id. We're using a template string to
    // help form the selector. Basically it says find me an element
    // with a "panel" class which also has an id that matches the id of
    // the button's data attribute which we just retrieved.
    const selector = `.panel[id="${id}"]`;

    // Select the `div` and, using classList, again add the
    // show class
    document.querySelector(selector).classList.add('show');
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to make an object in javascript 
Javascript :: .map method 
Javascript :: multiple checkbox validation in javascript 
Javascript :: search array for property js 
Javascript :: superagent vs axios 
Javascript :: jquery get last element with two class name 
Javascript :: angular how to use service in class 
Javascript :: javascript extend object 
Javascript :: create an express application 
Javascript :: jquery find element 
Javascript :: can i use splice in string of javascript 
Javascript :: what does return do in javascript 
Javascript :: javascript if function multiple conditions 
Javascript :: frames[i] javascript 
Javascript :: nestjs prisma 
Javascript :: number format reactjs 
Javascript :: es6 class 
Javascript :: if array javascript 
Javascript :: nesting arrays javascript 
Javascript :: notification react native 
Javascript :: numbers split 
Javascript :: JavaScript try...catch...finally Statement 
Javascript :: break out of map javascript 
Javascript :: private router react v6 
Javascript :: map & filter 
Javascript :: what is random state 
Javascript :: react infinte scroll 
Javascript :: react-bootstrap sidebar menu 
Javascript :: javascript Insert Item to Map 
Javascript :: validate country wise phone code javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =