javascript - highlight item in list generated by input text -
i have 2 lists below.
name:
- sm frost
- gm frost
email:
- smf@yahoo.com
- gmf@gmail.com
the 2 lists dynamically generated 2 input forms. trying highlight item in list. example, need highlight name 1 , email 1 @ same time. have tried use hover function , highlight css style no success.
following code in
http://jsfiddle.net/sanm/ycsdd04s/3/
html code:
bidders:<br> <br> name: <input type="text" id="name" class="input" placeholder="type name of bidder" > <br> <br> email: <input type="email" id="email" class="input" placeholder="example@example.com"> <br> <br> <input type='button' onclick='changetext2()' value='add'/> <br> <br> <br> bidders: <br> <p> name: </p> <ol id="demo"> </ol> <p> email: </p> <ol id="demo1"> </ol>
javascript code:
function changetext2(){ var name = document.getelementbyid('name').value; var email = document.getelementbyid('email').value; var node1=document.createelement("li"); var node2=document.createelement("li"); var textnode=document.createtextnode(name); var textnode1=document.createtextnode(email); node1.appendchild(textnode); node2.appendchild(textnode1); document.getelementbyid("demo").appendchild(node1); document.getelementbyid("demo1").appendchild(node2); }
i thankful help.
there lot of changes need structure, in order to:
- keep tags semantic.
- effectively use functions of jquery.
so have created 2 fiddles, see below, 2 events:
- hover
- click
does work? let me know in comments if there's issue.
hover
var count = 0; function changetext2() { count++; var name = $('#name').val(); var email = $('#email').val(); $("#names").append('<li data-n="' + count + '" class="n' + count + '">' + name + '</li>'); $("#emails").append('<li data-n="' + count + '" class="n' + count + '">' + email + '</li>'); } $("#names, #emails").on("mouseover", "li", function () { $(".n" + $(this).data("n")).addclass("active"); }); $("#names, #emails").on("mouseout", "li", function () { $(".n" + $(this).data("n")).removeclass("active"); });
* {font-family: 'segoe ui', sans-serif; margin: 0; padding: 0;} h3 {margin: 15px 0;} label {display: block;} label strong {font-weight: normal; display: inline-block; width: 75px; text-align: right; margin-right: 5px;} .btn {padding: 2px 5px; margin: 10px 10px 10px 85px;} ol li {margin-left: 2em;} ol li:hover, .active {background-color: #99f;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <h3>add bidder</h3> <label> <strong>name:</strong> <input type="text" id="name" class="input" placeholder="type name of bidder" /> </label> <label> <strong>email:</strong> <input type="email" id="email" class="input" placeholder="example@example.com" /> </label> <input type="button" onclick="changetext2()" value="add" class="btn" /> <h3>bidders</h3> <h4>names</h4> <ol id="names"></ol> <h4>emails</h4> <ol id="emails"></ol>
click
var count = 0; function changetext2() { count++; var name = $('#name').val(); var email = $('#email').val(); $("#names").append('<li data-n="' + count + '" class="n' + count + '">' + name + '</li>'); $("#emails").append('<li data-n="' + count + '" class="n' + count + '">' + email + '</li>'); } $("#names, #emails").on("click", "li", function () { $(".n" + $(this).data("n")).toggleclass("active"); });
* {font-family: 'segoe ui', sans-serif; margin: 0; padding: 0;} h3 {margin: 15px 0;} label {display: block;} label strong {font-weight: normal; display: inline-block; width: 75px; text-align: right; margin-right: 5px;} .btn {padding: 2px 5px; margin: 10px 10px 10px 85px;} ol li {margin-left: 2em;} .active {background-color: #99f;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <h3>add bidder</h3> <label> <strong>name:</strong> <input type="text" id="name" class="input" placeholder="type name of bidder" /> </label> <label> <strong>email:</strong> <input type="email" id="email" class="input" placeholder="example@example.com" /> </label> <input type="button" onclick="changetext2()" value="add" class="btn" /> <h3>bidders</h3> <h4>names</h4> <ol id="names"></ol> <h4>emails</h4> <ol id="emails"></ol>
Comments
Post a Comment