javascript - Drop down menu on click on variable id -
i have page grid of images. want drop down appear on whatever image clicked. code have allows specific id, rather id. code have works fine, it's id specific.
css:
.wide { width:50px; } .high { height:50px; } table { border-collapse:collapse; margin:0px; padding:0px; } td { margin:0px; padding:0px; } td img { display:block; } .messagepop { background-color:#ffffff; border:1px solid #999999; cursor: pointer; display:none; margin-top: 5px; padding-left:2px; position:absolute; text-align:left; width:50px; z-index:50; font-family:tahoma; } .messagepop p, .messagepop.div{ border-bottom: 1px solid #efefef; line-height:1.3em; display:inline; } .close { color:blue; }
html:
<td class="wide high"> <div class="messagepop pop" id="x1y1pop"> <p>dfkj</p> <p>alksdjh</p> <p class="close">cancel</p> </div> <img alt="test" src="images/gray.jpg" class="wide high" id="x1y1" /> </td>
jquery:
function deselect(e) { $('#x1y2pop').slidefadetoggle(function () { e.removeclass('selected'); }); } $(function () { $('#x1y2').on('click', function () { if ($(this).hasclass('selected')) { deselect($(this)); } else { $(this).addclass('selected'); $('#x1y2pop').slidefadetoggle(); } return false; }); $('.close').on('click', function () { deselect($('#x1y2')); return false; }); }); $.fn.slidefadetoggle = function (easing, callback) { return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback); };
i need $('#x1y2pop').slidefadetoggle
, $('#x1y2').on
, $('#x1y2pop').slidefadetoggle();
, deselect($('#x1y2'));
able accept variables rather static ids.
$(selector).on('click', function(){
where selector
variable. possible?
if understanding correctly, can fix problem best targeting classes instead of ids. additionally, can use jquery's .prev()
target related <div>
.
something this:
$(function () { $('.cantoggle img').on('click', function () { if ($(this).hasclass('selected')) { deselect($(this)); } else { $(this).addclass('selected'); $(this).prev(".messagepop.pop").slidefadetoggle(); } return false; }); $('.close').on('click', (function (e) { e.preventdefault(); var pop = $(this).closest(".messagepop.pop"); pop.slidefadetoggle(function() { pop.next("img").removeclass('selected'); }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table class="cantoggle"> <tr> <td class="wide high"> <div class="messagepop pop" id="x1y1pop"> <p>dfkj</p> <p>alksdjh</p> <p class="close">cancel</p> </div> <img alt="test" src="images/gray.jpg" class="wide high" id="x1y1" /> </td> <td class="wide high"> <div class="messagepop pop" id="x1y2pop"> <p>dfkj</p> <p>alksdjh</p> <p class="close">cancel</p> </div> <img alt="test" src="images/gray.jpg" class="wide high" id="x1y2" /> </td> <tr> </table>
Comments
Post a Comment