javascript - How to get class index from hovering Div -
i have been trying create system when hover on specific div, specific text relating div appears. each div in same class, info @ different div corresponding indexes. wondering if there way class index of each div hovering on them in order show hidden information them. (by showing hidden info divs).
<!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> var allpeople = []; function win (name, info){ this.name = name; this.info = info; } allpeople[0] = new win ("shelly", "is cool"); allpeople[1] = new win ("brandon", "likes golf"); allpeople[2] = new win ("steve", "plays football"); allpeople[3] = new win ("mia", "is cook"); var = 0; $('document').ready(function(){ for (i = 0; < allpeople.length; i++){ $("body").append("<div class='people'> " + allpeople[i].name +" </div>"); } for (i = 0; < allpeople.length; i++){ $("body").append("<div class='info'> " + allpeople[i].info +" </div>"); } }); </script> <style> body { background-color: lightblue; } div { background-image: url("http://themes.wdfiles.com/local--files/semi-trans/semi-transbgtransparent.png"); color: white; padding: 2%; margin: 2%; border: 3px white solid; } .info { display: none; border-color: red; } </style> </head> <body> </body> </html>
a simple solution can based on saving index of each div @ creation time data attribute like:
$("body").append("<div class='people' data-index='" + +"'> " + allpeople[i].name +" </div>"); var allpeople = []; function win (name, info){ this.name = name; this.info = info; } allpeople[0] = new win ("shelly", "is cool"); allpeople[1] = new win ("brandon", "likes golf"); allpeople[2] = new win ("steve", "plays football"); allpeople[3] = new win ("mia", "is cook"); var = 0; $('document').ready(function(){ (i = 0; < allpeople.length; i++){ $("body").append("<div class='people' data-index='" + +"'> " + allpeople[i].name +" </div>"); } (i = 0; < allpeople.length; i++){ $("body").append("<div class='info' data-index='" + +"'> " + allpeople[i].info +" </div>"); } $('.people').hover(function(e) { $('.info').eq($(this).data('index')).show(); }, function(e) { $('.info:visible').hide(); }); }); body { background-color: lightblue; } div { background-image: url("http://themes.wdfiles.com/local--files/semi-trans/semi-transbgtransparent.png"); color: white; padding: 2%; margin: 2%; border: 3px white solid; } .info { display: none; border-color: red; } <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> you can use jquery.index():
var allpeople = []; function win (name, info){ this.name = name; this.info = info; } allpeople[0] = new win ("shelly", "is cool"); allpeople[1] = new win ("brandon", "likes golf"); allpeople[2] = new win ("steve", "plays football"); allpeople[3] = new win ("mia", "is cook"); var = 0; $('document').ready(function(){ (i = 0; < allpeople.length; i++){ $("body").append("<div class='people'> " + allpeople[i].name +" </div>"); } (i = 0; < allpeople.length; i++){ $("body").append("<div class='info'> " + allpeople[i].info +" </div>"); } $('.people').hover(function(e) { $('.info').eq($(this).index() % 4).show(); }, function(e) { $('.info').eq($(this).index() % 4).hide(); }); }); body { background-color: lightblue; } div { background-image: url("http://themes.wdfiles.com/local--files/semi-trans/semi-transbgtransparent.png"); color: white; padding: 2%; margin: 2%; border: 3px white solid; } .info { display: none; border-color: red; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> a different approach can based on filtering array elements (array.prototype.filter()) in order index of corresponding div.
var allpeople = []; function win (name, info){ this.name = name; this.info = info; } allpeople[0] = new win ("shelly", "is cool"); allpeople[1] = new win ("brandon", "likes golf"); allpeople[2] = new win ("steve", "plays football"); allpeople[3] = new win ("mia", "is cook"); var = 0; $('document').ready(function(){ (i = 0; < allpeople.length; i++){ $("body").append("<div class='people'> " + allpeople[i].name +" </div>"); } (i = 0; < allpeople.length; i++){ $("body").append("<div class='info'> " + allpeople[i].info +" </div>"); } $('.people').hover(function(e) { var txt = this.textcontent.trim(); var peopleinfoidx = 0; allpeople.foreach(function(ele, idx) { if (ele.name == txt) peopleinfoidx = idx; }); $('.info').eq(peopleinfoidx).show(); }, function(e) { $('.info:visible').hide(); }); }); body { background-color: lightblue; } div { background-image: url("http://themes.wdfiles.com/local--files/semi-trans/semi-transbgtransparent.png"); color: white; padding: 2%; margin: 2%; border: 3px white solid; } .info { display: none; border-color: red; } <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
Comments
Post a Comment