0

This is my html code:

<!DOCTYPE html>
<html>
<head>
<title>Testing Event Listener</title>
</head>
 <body>
   <h1>Testing Event Listener</h1><br>
   <input id="userinput" type="text" placeholder="Enter Elements">
   <button id="enter">Enter</button>
   <ul>
       <li>Javascript</li>
       <li>Python</li>
       <li>Ruby On Rails</li>
   </ul>
<script type="text/javascript" src="script.js"></script>
</body>
</html>   

and this is my javascript code :

var input= document.getElementById("userinput");
var button= document.getElementById("enter");
var ul= document.querySelector("ul");
function inputLenght(){
    return input.value.lenght;
}
function creatListElement(){
    var li=document.createElement("li");
    li.appendChild(document.createTextNode(input.value));    
    ul.appendChild(li);
    input.value="";
}
function addListAfterClick(){
    if(inputLenght> 0){
        creatListElement();
    }
}

button.addEventListener("click",addListAfterClick); 

This program is not showing any error but it is supposed to add item inside <ul> and a <li> that we write inside placeholder but it is not doing any thing

1
  • If you recived a solution in one of answers below - please mark it as correct answer. Otherwise please specify where you have more problems. Commented Aug 20, 2018 at 13:06

4 Answers 4

1

var input= document.getElementById("userinput");
var button= document.getElementById("enter");
var ul= document.querySelector("ul");
function inputLenght(){
    return input.value.length; // spelt length wrong here
}
function creatListElement(){
    var li=document.createElement("li");
    li.innerText = input.value;    
    ul.appendChild(li);
    input.value="";
}
function addListAfterClick(){
    if(inputLenght() > 0){ //Missing brackets from "inputLength"
        creatListElement();
    }
}

button.addEventListener("click",addListAfterClick); 
<!DOCTYPE html>
<html>
<head>
<title>Testing Event Listener</title>
</head>
 <body>
   <h1>Testing Event Listener</h1><br>
   <input id="userinput" type="text" placeholder="Enter Elements">
   <button id="enter">Enter</button>
   <ul>
       <li>Javascript</li>
       <li>Python</li>
       <li>Ruby On Rails</li>
   </ul>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

You need to call inputLenght in addListAfterClick(), and return input.value.length; instead of input.value.lenght; in inputLength()

var input= document.getElementById("userinput");
var button= document.getElementById("enter");
var ul= document.querySelector("ul");
function inputLenght(){
    return input.value.length; // <- typo here
}
function creatListElement(){
    var li=document.createElement("li");
    li.appendChild(document.createTextNode(input.value));    
    ul.appendChild(li);
    input.value="";
  console.log('foo')
}
function addListAfterClick() {
    if(inputLenght()> 0){ // <- need to call inputLength()
        creatListElement();
    }
}

button.addEventListener("click", addListAfterClick); 

Comments

0

You have a typo in your JavaScript:

return input.value.lenght;

Should be:

function inputLenght(){ return input.value.length; }.

Also need to call the function in your handler:

inputLength()

1 Comment

The title of the function should also be 'inputLength', but you call the name consistently with the typo so isn't a problem there
0

This is not working because in addListAfterClick you are checking

if(inputLenght > 0){

instead of

if(inputLenght() > 0){

You are not calling the function, you are checking "if function if greater than zero", which is always false.

To test this - define function f1() {} in console and then run f1 > 0.

--- edit ---

Btw - a better name of this function would be getInputLength, so you know that you'll get some return value, and there's no typo in it (ght -> gth)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.