Created
November 5, 2013 12:00
-
-
Save chikoski/7318055 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .hidden{ | |
| display: none; | |
| } | |
| .visible{ | |
| display: inline; | |
| background-color: #f99; | |
| padding: 4px 1em; | |
| color: #333; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset=utf-8 /> | |
| <title>JS Bin</title> | |
| </head> | |
| <body> | |
| <div> | |
| <input type="text" id="name"> | |
| <button id="btn">追加</button> | |
| <p id="error" class="hidden">その名前は既に存在します</p> | |
| </div> | |
| <ul id="list"></ul> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var nameList = []; | |
| var btn = document.getElementById("btn"); | |
| var input = document.getElementById("name"); | |
| var list = document.getElementById("list"); | |
| var error = document.getElementById("error"); | |
| var showNameList = function(list){ | |
| list.innerHTML = "<li>" + nameList.join("</li><li>") + "</li>"; | |
| }; | |
| var indexOf = function(name){ | |
| var i = 0; | |
| while(i < nameList.length){ | |
| if(name == nameList[i]){ | |
| return i; | |
| } | |
| i = i + 1; | |
| } | |
| return -1; | |
| }; | |
| var exists = function(name){ | |
| return indexOf(name) >= 0; | |
| }; | |
| var showErrorMessage = function(){ | |
| error.setAttribute("class", "visible"); | |
| }; | |
| var hideErrorMessage = function(){ | |
| error.setAttribute("class", "hidden"); | |
| }; | |
| btn.onclick = function(){ | |
| hideErrorMessage(); | |
| if(!exists(input.value)){ | |
| nameList.push(input.value); | |
| showNameList(list); | |
| }else{ | |
| showErrorMessage(); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment