I get stuck with callbacks , i searched to find challenges to practice this concept but i don't find any thing , can anyone help me

//getting DOM elements//

let firstName=document.getElementById('firstName').value;

let secondName=document.getElementById('secondName').value;

let loveResulat=document.getElementById('love');

let btn=document.getElementById('btn');

// love calculator functionality//

//1. get random number//

function getrandom(maxLimit = 99) {

    let rand = Math.random() * maxLimit;

    return Math.floor(rand);

   }

     

  //2.form validation//

  firstName.addEventListener('submit',validation);

  secondName.addEventListener('submit',validation);

  function validation(){ 

      'use strict';

      e.preventdefault();

      if(firstName==''){

         console.log('Please enter your firstName')

      }

    else if(firstName.length<=3){

        alert('the min is 2 items')

    }

    else if(!NaN(firstName)){

        alert('number not allowed');

    }

    if(secondName==''){

        console.log('please enter your name')

    }

    else if(secondName.length<=3){

alert('the min is 2')

    }

    else if(!NaN(secondName)){

        alert('number are not allowed')

    }

  }

 validation();

Could you describe your problem? What is happening? What should be happening? What troubleshooting have you tried?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

the problem is that i can’t attach addEventListener to firstName element , it show me this error in the console : Uncaught TypeError: firstName.addEventListener is not a function

It’s telling you that firstName is not an element and thus you cannot use the addEventListener method on it. Look at where you are declaring firstName:

let firstName=document.getElementById('firstName').value;

What is the value of firstName after this? What is its type?

You are using this variable correctly in the following:

if(firstName==''){ ... }

Do you see why you can’t attach an event listener to firstName? You are attempting to use this variable as both a string and an element but only one is correct.

thank you . now , i understand the problem

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.