addEventListener and control flow

Basically, when I put an addEventListener in my code, does the script stop executing until the event happens and gets listened or the code keeps getting executed no matter what?

I mean, if an event that I want to listen and that is placed at the start of my script does not happen, does it block my whole script?

If you can, don’t just answer yes or no, I would be glad if you could tell what the interpreter does in those situations.

Perform Multiplication and Division in Moment.Js

I want to divide the value of duration by 9 and then multiply it by 10. I am currently getting a not a type of integer error. Any idea how I can go about this?

let start = 1977
let end = 1985
let duration = moment(end.toString()).unix() - moment(start.toString()).unix();
let newduration = (duration/9) * 10;
let newvalue = moment(end.toString()).subtract(newduration, "second");

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.j

Share url on direct message on Linkedin does not work when user is not logged in and in mobile

I’m trying to share an url with a direct message on Linkedin.
Send.js:

export default function Send() {
    const handleClick = async () => {
      window.open(
        `https://www.linkedin.com/sharing/share-offsite/?url=my_url`
      );
    };
    return (
      <>
        <h3>Share link</h3>
        <button onClick={handleClick}>Share</button>
      </>
    );
  }

When I open it on desktop with Linkedin already logged in, I got the page to choose between share in a post or send in private message. However, if I’m not signed in, the login page is displayed and after I enter my credentials, I’m redirecting to the homepage not to the page to share the url.
On Mobile, when I click the button, the linkedin app is not proposed to use it instead of using the web app.
How can I fix those issues?

animating(playing) Lottie files with gsap/scrollTrigger

we’re trying to animate(play) the Lottie files with gsap/scrollTrigger. The scroll works fine until I reach the part where we should use scrollTrigger to animate some elements.

This is the code that we have for now. I may also set a bounty for the question if it doesn’t get answered in 2 days. You can simply put these code snippets in codepen to use them. Or if you want it to be easier search my name amini-py in codepen.

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/gsap.min.js" integrity="sha512-VEBjfxWUOyzl0bAwh4gdLEaQyDYPvLrZql3pw1ifgb6fhEvZl9iDDehwHZ+dsMzA0Jfww8Xt7COSZuJ/slxc4Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.10.4/ScrollTrigger.min.js" integrity="sha512-v8B8T8l8JiiJRGomPd2k+bPS98RWBLGChFMJbK1hmHiDHYq0EjdQl20LyWeIs+MGRLTWBycJGEGAjKkEtd7w5Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/@lottiefiles/[email protected]/dist/lottie-interactivity.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@lottiefiles/[email protected]/dist/lottie-player.min.js"></script>
  <div class="wrapper-child">
    <div class="child">
      <h5>First</h5>
      <p>lorem ipsum sit amet sssssssssssssssssssssss</p>
    </div>
    <div class="child">
      <h5>Second</h5>
      <p>lorem ipsum sit amet sssssssssssssssssssssss</p>
    </div>
    <div class="child">
      <h5>Thirds</h5>
      <p>lorem ipsum sit amet sssssssssssssssssssssss</p>
    </div>
    <div class="child">
      <h5>Fourth</h5>
      <p>lorem ipsum sit amet sssssssssssssssssssssss</p>
    </div>
    <div class="child">
      <h5>Fifth</h5>
      <p>lorem ipsum sit amet sssssssssssssssssssssss</p>
    </div>
</section>
  width: 90vw;
  height: 250px;
  overflow-y: hidden;
  display: flex;
}

.wrapper-child {
  border: 1px solid red;
  width: 50%;
}
  player: "#firstLottie",
  mode: "scroll",
  actions: [
    {
      visibility: [0, 1.0],
      type: "seek",
      frames: [0, 300]
    }
  ]
});

gsap.registerPlugin(ScrollTrigger);

const sections = gsap.utils.toArray(".child");

gsap.to(sections, {
  yPercent: -100 * (sections.length - 1),
  ease: "none",
  scrollTrigger: {
    trigger: ".wrapper",
    pin: true,
    scrub: 1,
    start: "top center",
    end: () => "+=" + document.querySelector(".wrapper").offsetHeight
  }
});

Why Progress bar don’t stop at my value placed in the input box?

I have the following code:

var i = 0;

var My_Button = document.getElementById("btn1")

var Numerica = parseInt(My_Button)


function update() {
  var element = document.getElementById("myprogressBar");
  var width = 1;
  var identity = setInterval(scene, 10);
  function scene() {
    if (width >= Numerica) {
      clearInterval(identity);
    } else {
      width++;
      element.style.width = width + '%';
    }
  }
}
#Progress_Status {
  width: 50%;
  background-color: #ddd;
}

#myprogressBar {
  width: 2%;
  height: 20px;
  background-color: red;
}
<h3>Example of Progress Bar Using JavaScript</h3>
    
<input style="height:50px; width:50px; font-size:30px" type = text id = "btn1" name = "btn10" > <span id ="option1" style="font-size:30px">Option1</span>

<p>Pogress Bar</p>

<div id="Progress_Status">
  <div id="myprogressBar"></div>
</div>

<br>
<button onclick="update()">Start Download</button>

I want to decide a number inside the Input box and make to that the bar progress until that decided number.
Why is the previously showed code the bar never stop?
I am transforming right the value into an integer

how to close info box on a kml file when clicking on another kml file in the same map, then open the new info box at the clicked element

I have google maps (Javascript) that sometimes has multi-kml files. whenever an element clicks on the kml file it shows an info box ..and when another element on the same kml file clicked again it closes the already opened info box and opens the new clicked one.. but when the second click is on another kml file.. it simply opens 2 info boxes on the same map .. one from the first kml file and the other one from the second kml file.

Here is how am applying the kml file to the map

var src = "https://{{domain}}{{single_layer.file.url}}";

var kml_layer = new google.maps.KmlLayer(src, {
     suppressInfoWindows: false,
     preserveViewport: true,
     map: map
     });

and then am adding a click listener to it that takes the clicked point information and does some other stuff like sending an AJAX call, etc.

kml_layer.addListener('click', (kmlEvent) => {
     var clickedPoint = kmlEvent.latLng;
      });

what I need to do is to add something in this click listener that make sure to close all already open info boxes before opening the new one .

content disappearing when navbar is pushed up

I want a navbar to disappear wright after the website has loaded

window.onload = function(){
        setTimeout(function (){
            document.getElementById("navbar").style.top = "-500px";
        },2000);
    }``

The rest of the content on my website (Cards generated with Bootstrap) disappears.
when removing the brackets after “function” the content appears, but pushing up the navbar doesn´t anymore.

Module parse failed: Unexpected token You may need an appropriate loader to handle this file type. @splinetool/runtime

I am using a react portfolio template. I am new to react and webpack. I am trying to use Spline to add a 3D model to my page, but I’m getting the following error.

./node_modules/@splinetool/runtime/build/runtime.js 19847:16
Module parse failed: Unexpected token (19847:16)
You may need an appropriate loader to handle this file type.
| function fI(r, e) {
|   var t = typeof r; >   return e = e ?? hI, !!e && (t == "number" || t != "symbol" && dI.test(r)) && r > -1 && r % 1 == 0 && r < e;
| }
|

I have also installed react-script package and typescript which are required for spline to work.

My package.json file:

  "name": "portfolio",
  "version": "0.1.0",
  "private": true,
  "homepage": "/",
  "basename": "/",
  "dependencies": {
    "@babel/core": "7.4.3",
    "@splinetool/react-spline": "^2.2.1",
    "@splinetool/runtime": "^0.9.73",
    "@svgr/webpack": "4.1.0",
    "@typescript-eslint/eslint-plugin": "1.6.0",
    "@typescript-eslint/parser": "1.6.0",
    "babel-eslint": "10.0.1",
    "babel-jest": "^24.9.0",
    "babel-loader": "8.0.5",
    "babel-plugin-named-asset-import": "^0.3.7",
    "babel-preset-react-app": "^9.1.2",
    "bootstrap": "^4.5.3",
    "camelcase": "^5.2.0",
    "case-sensitive-paths-webpack-plugin": "2.2.0",
    "css-loader": "2.1.1",
    "dotenv": "6.2.0",
    "dotenv-expand": "4.2.0",
    "eslint": "^5.16.0",
    "eslint-config-react-app": "^4.0.1",
    "eslint-loader": "2.1.2",
    "eslint-plugin-flowtype": "2.50.1",
    "eslint-plugin-import": "2.16.0",
    "eslint-plugin-jsx-a11y": "6.2.1",
    "eslint-plugin-react": "7.12.4",
    "eslint-plugin-react-hooks": "^1.7.0",
    "file-loader": "3.0.1",
    "fs-extra": "7.0.1",
    "google-map-react": "^1.1.7",
    "html-webpack-plugin": "4.0.0-beta.5",
    "identity-obj-proxy": "3.0.0",
    "is-wsl": "^1.1.0",
    "jest": "24.7.1",
    "jest-environment-jsdom-fourteen": "0.1.0",
    "jest-resolve": "24.7.1",
    "jest-watch-typeahead": "0.3.0",
    "mini-css-extract-plugin": "0.5.0",
    "optimize-css-assets-webpack-plugin": "5.0.1",
    "pnp-webpack-plugin": "1.2.1",
    "postcss-flexbugs-fixes": "4.1.0",
    "postcss-loader": "3.0.0",
    "postcss-normalize": "7.0.1",
    "postcss-preset-env": "6.6.0",
    "postcss-safe-parser": "4.0.1",
    "react": "^16.14.0",
    "react-accessible-accordion": "^3.3.3",
    "react-animate-on-scroll": "^2.1.5",
    "react-app-polyfill": "^1.0.6",
    "react-bootstrap": "^1.4.0",
    "react-countup": "^4.3.3",
    "react-dev-utils": "^9.1.0",
    "react-dom": "^16.14.0",
    "react-helmet": "^5.2.1",
    "react-icons": "^3.11.0",
    "react-iframe": "^1.8.0",
    "react-image-lightbox": "^5.1.1",
    "react-modal-video": "^1.2.6",
    "react-parallax": "^3.1.2",
    "react-particles-js": "^2.7.1",
    "react-reveal": "^1.2.2",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^5.0.1",
    "react-scroll-up": "^1.3.5",
    "react-scrollspy": "^3.4.3",
    "react-slick": "^0.24.0",
    "react-tabs": "^3.1.1",
    "react-text-loop": "^2.3.0",
    "react-tilt": "^0.1.4",
    "react-video-tag": "0.0.10",
    "react-visibility-sensor": "^5.1.1",
    "resolve": "1.10.0",
    "sass-loader": "^7.3.1",
    "semver": "6.0.0",
    "slick-carousel": "^1.8.1",
    "style-loader": "0.23.1",
    "terser-webpack-plugin": "1.2.3",
    "ts-pnp": "1.1.2",
    "url-loader": "1.1.2",
    "webpack": "4.29.6",
    "webpack-dev-server": "^3.11.0",
    "webpack-manifest-plugin": "2.0.4",
    "workbox-webpack-plugin": "4.2.0"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jest-environment-jsdom-fourteen",
    "transform": {
      "^.+\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "^.+\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\]node_modules[/\\].+\.(js|jsx|ts|tsx)$",
      "^.+\.module\.(css|sass|scss)$"
    ],
    "modulePaths": [],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\.module\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ]
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  },
  "devDependencies": {
    "@babel/preset-react": "^7.18.6",
    "@babel/runtime": "^7.18.6",
    "sass": "^1.53.0",
    "typescript": "^4.7.4"
  }
}

Part of my React code:

class SliderOne extends Component {
  render() {
    return (
 <div className="slider-activation">
<div className="inner">
<Spline scene="https://prod.spline.design/zijR8dif6Kg3gTyC/scene.splinecode" />
</div>
</div>```

why i’m getting assertion error in JavaScript Filter App

im getting assertion error please solve this issue

my html code

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="index.css" />
</head>

<body onload="event.preventDefault();onPageLoad()">
    <h1>Student Details</h1>
    <div>
        <label>Subject</label>
        <select id="subjects">
            <option id="opt0" value="">-- Select Subject --</option>
            <option id="opt1" value="English">English</option>
            <option id="opt2" value="Maths">Maths</option>
            <option id="opt3" value="Science">Science</option>
            <option id="opt4" value="SocialScience">Social Science</option>
        </select>
        <label>Filter By</label>
        <input type="radio" id="above" name="mode" value="above" onclick="filterBy()" checked />
        <label for="above">above</label>
        <input type="radio" id="below" name="mode" value="below" onclick="filterBy()" />
        <label for="below">below</label>
        <input type="radio" id="between" name="mode" value="between" onclick="filterBy()" />
        <label for="between">between</label>
        <input id="mark" type="number">
        <label id="to" style="display: none;">to</label>
        <input id="maxMark" type="number" style="display: none;" id="inputMark">
        <button id="filter" onclick="filterClick()">Filter</button>
        <button id="clear" onclick="Clear()">Clear</button>
    </div>
    <table id="studentTable">
        <thead>
            <tr>
                <th>S.No</th>
                <th>Name</th>
                <th>English</th>
                <th>Maths</th>
                <th>Science</th>
                <th>Social Science</th>
            </tr>
        </thead>
        <tbody id="studentData">

        </tbody>
    </table>

</body>
<script src="./js/script.js"></script>

</html>

my js code here

  {
      id: 1,
      name: "Janu",
      English: 50,
      Maths: 86,
      Science: 77,
      SocialScience: 88
  },
  {
      id: 2,
      name: "Thanu",
      English: 75,
      Maths: 96,
      Science: 67,
      SocialScience: 91
  },
  {
      id: 3,
      name: "Tara",
      English: 90,
      Maths: 35,
      Science: 86,
      SocialScience: 100
  },
  {
      id: 4,
      name: "Glen",
      English: 79,
      Maths: 68,
      Science: 77,
      SocialScience: 78
  },
  {
      id: 5,
      name: "Zara",
      English: 80,
      Maths: 85,
      Science: 96,
      SocialScience: 68
  }
]
var modes;
var mode_value, subjectOption_value,subjectOption_index;
var table,tr,txtValue,filter;
var cells,cellVal;
function onPageLoad() {
  // code goes here to display table on page loads
  table = document.getElementById("studentData");

  for(i = 0; i < data.length;i++) {
      var row = table.insertRow(i);
      row.insertCell(0).innerHTML = data[i].id;
      row.insertCell(1).innerHTML = data[i].name;
      row.insertCell(2).innerHTML = data[i].English;
      row.insertCell(3).innerHTML = data[i].Maths;
      row.insertCell(4).innerHTML = data[i].Science;
      row.insertCell(5).innerHTML = data[i].SocialScience;
  }
  
  if(document.getElementById("above").checked) {
      mode_value = "above";
  }
}

function filterBy() {
  // code goes here to select filter by option
  var modes = document.getElementsByName("mode");
  for(var i = 0; i < modes.length; i++){
      if(modes[i].checked){
          mode_value = modes[i].value;
      }
  }
  
  if(mode_value == "between") {
      document.getElementById("to").style.display = '';
      document.getElementById("maxMark").style.display = '';
  }
}

function Clear() {
  // code goes here to clear filter
   document.getElementById("mark").value = '';
document.getElementById("subjects").value = '';

var input, filter, table, tr, td, i;
input = document.getElementById("clear");
filter = input.value.toUpperCase();
table = document.getElementById("studentTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
  td = tr[i].getElementsByTagName("td")[0];
  if (td) {
    if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
      tr[i].style.display = "";
    } else {
      tr[i].style.display = "none";
    }
  }       
}
}

function filterClick() {
  // code goes here to handle filter button
   tr = table.getElementsByTagName("tr");
  input = document.getElementById("mark");
  input1 = document.getElementById("maxMark");
  
  subjectOption_value = document.getElementById("subjects").value;
  subjectOption_index = document.getElementById("subjects").selectedIndex;
  
  if(subjectOption_value != "") {
      for (i = 0; i < tr.length; i++) {
          cells = tr.item(i).cells;
          cellVal = cells.item(subjectOption_index+1).innerHTML;
          
          if (mode_value == 'above') {
              if(cellVal > input.value) {
                  tr[i].style.display = "";
              } else {
                  tr[i].style.display = "none";
              }
                  
          } else if (mode_value == 'below') {
              if(cellVal < input.value) {
                  tr[i].style.display = "";
              } else {
                  tr[i].style.display = "none";
              }
          } else if(mode_value == 'between') {
              if(cellVal >= input.value && cellVal < input1.value) {
                  tr[i].style.display = "";
              } else {
                  tr[i].style.display = "none";
              }
          }
      }
  }
}

code

This is the error im facing

[email protected] test /projects/challenge

NODE_ENV=test ./node_modules/.bin/mocha test –exit

App is running in port 8001

Filter app

✓ OnLoad

1) above

2) below

3) between

4) on clicking clear button

✓ on Empty field (111ms)

2 passing (2s)

4 failing

  1. Filter app

    above:

    AssertionError: expected false to be true

    • expected – actual

    -false

    +true

    at Context. (test/test.js:80:37)

    at

    at process._tickCallback (internal/process/next_tick.js:189:7)

  2. Filter app

    below:

    AssertionError: expected false to be true

    • expected – actual

    -false

    +true

    at Context. (test/test.js:101:37)

    at

    at process._tickCallback (internal/process/next_tick.js:189:7)

  3. Filter app

    between:

    AssertionError: expected false to be true

    • expected – actual

    -false

    +true

    at Context. (test/test.js:121:37)

    at

    at process._tickCallback (internal/process/next_tick.js:189:7)

  4. Filter app

    on clicking clear button:

    AssertionError: expected false to be true

    • expected – actual

    -false

    +true

    at Context. (test/test.js:139:37)

    at

    at process._tickCallback (internal/process/next_tick.js:189:7)

npm ERR! Test failed. See above for more details.

Attach mouse movement to a div with Iframe inside

In my JS code, I’ve added a EventListener on mousemove & touchmove, which makes it so that my div follows the cursor. (as such)

CSS:

  <style>
    body {
padding: 0;
margin: 0;
height: 100vh;
background: linear-gradient(135deg, #8fc7f1, #7173f5);
overflow: hidden;
}
#my-div {
width: 300px;
height: 250px;
background-color: #ffffff;
position: absolute;
}
</style>

HTML:

<div id="my-div">
  <h1>Hello</h1>
</div>

JAVASCRIPT:

let myDiv = document.getElementById("my-div");
//Detect touch device
function isTouchDevice() {
  try {
    //We try to create TouchEvent. It would fail for desktops and throw error
    document.createEvent("TouchEvent");
    return true;
  } catch (e) {
    return false;
  }
}

const move = (e) => {
  //Try, catch to avoid any errors for touch screens (Error thrown when user doesn't move his finger)
  try {
    //PageX and PageY return the position of client's cursor from top left of screen
    var x = !isTouchDevice() ? e.pageX : e.touches[0].pageX;
    var y = !isTouchDevice() ? e.pageY : e.touches[0].pageY;
  } catch (e) {}
  //set left and top of div based on mouse position
  myDiv.style.left = x - 150 + "px";
  myDiv.style.top = y - 120 + "px";
  // myDiv.style.opacity = 0;
  // console.log(myDiv.getBoundingClientRect())
};
//For mouse
document.addEventListener("mousemove", (e) => {
  move(e);
  console.log("Mouse")
});

//For touch
document.addEventListener("touchmove", (e) => {
  move(e);
});

JSFIDDLE CODE

What I’m trying to do is to add a iframe inside my div and the div should still follow the cursor, but when I add the iframe inside the div, then the EventListener stops working and my div stops following the cursor. I’m not sure what the issue is. (as such)

<div id="my-div">
    <iframe src="https://example.com/" width="300" height="250">
</div>

Any help and suggestion is appreciated!

How to use an option in a selection element to run a function depending on the selected option

I aim to use a selected option on a selector element to run a specific function.

This is what I have written in html.

     <select id="selector" class="selector">
        <option selected>Preset</option>
        <option value="item1" class="item1" id="item1" >item1</option>
        <option value="item2" class="item2" id="item2" >item2</option>
        <option value="item3" class="item3" id="item3" >item3</option>
      </select>

This is what I have written in JS.

let array01 = [{id:”07-01″}, {id:”05-01″}, {id:”05-03″}, {id:”05-05″}, {id:”05-07″}, {id:”05-09″}, {id:”05-11″}, {id:”05-13″}, {id:”05-05″}, {id:”06-05″}, {id:”07-08″}, {id:”07-11″}, {id:”06-13″}]

const test = array01.map(item => item.id);

$(“#item1”).on(“click”, function(){

test.forEach(element => (("#" + element).addClass("mySpecialClass")));

});

But I can not figure out what to do to make it work.