jquery - Else clause is running always if I remove (else code) everything working fine -
if remove else
part of code working fine if
statement correct executing else
part also.
why happening , how can resolve this?
please me, new in javascript.
<!doctype html> <html> <head> <title>priject 2</title> </head> <body> <label>name: <input type="text" id="name"> </label> <input type="submit" id="fetch"> <br /> <dl> <dt>age</dt> <dd class="age">-</dd> <dt>location</dt> <dd class="location">-</dd> <dt>job</dt> <dd class="job ">-</dd> <script src="jquery-1.11.1.js"></script> <script></script> </dl> <script src="jquery-1.11.1.js"></script> <script> var people=[{ name:"alex", age:20, location:"canada", job:"web developer" }, { name:"aliana", age:21, location:"japan", job:"teacher" } ] for(i=0;i<people.length;i++){ result(people[i]) } function result(data){ $("#fetch").on("click",function(){ var name=$("#name").val(); if(data.name ===name ){ $(".age").text(" "); $(".job").text(" "); $(".location").text(" "); $(".age").append(data.age); $(".job").append(data.job); $(".location").append(data.location); } else if(data.name != name) { $("body").text(" ") noresult(); } }) } function noresult(){ var noresult="<div><p>name not in database </p></div>"; $("body").append(noresult); } </script> </body> </html>
pretty bad in code. refactored it.
in code, did :
a = [1,2,3,4]
input = 1
loop every item in array
input = 1 ? yes => fetch , append text
input = 2 ? no => remove text show error
input = 3 ? no => remove text show error
input = 4 ? no => remove text show error
at end got error submitted name one.
every time click on button "function" $("#fetch").on("click",function(){
called. can want in it.
note : use .html()
replace content, not .text("")
, .append(x)
.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!doctype html> <html> <head> <title>priject 2</title> </head> <body> <label>name: <input type="text" id="name"> </label> <input type="submit" id="fetch"> <br /> <div id="result"> <dl> <dt>age</dt> <dd class="age">-</dd> <dt>location</dt> <dd class="location">-</dd> <dt>job</dt> <dd class="job">-</dd> </dl> </div> <div id="noresult"> <p>name not in database </p> </div> <script> var people=[{ name:"alex", age:20, location:"canada", job:"web developer" }, { name:"aliana", age:21, location:"japan", job:"teacher" } ] $("#noresult").hide(); $("#fetch").on("click",function(){ var name=$("#name").val(); // loop through each person in array , "filter" matching name new array. var foundpeople = people.filter(function(person){ return name === person.name; }); // name found => length = 1, not found => length = 0 if(foundpeople.length !== 0){ result(foundpeople[0]) } else { noresult() } }) function result(people){ $(".age").text(people.age); $(".job").text(people.job); $(".location").text(people.location); $("#result").show(); $("#noresult").hide(); } function noresult(){ $("#result").hide(); $("#noresult").show(); } </script> </body> </html>
Comments
Post a Comment