1

So let's say I have a menu from an array (pmmain) that is generated like this:

$("#pm-page-main") .append("<div class=\"main-box\" id=\"m-box"+b+"\"><div class=\"title-box\"><span class=\"reg-wht-bold\">" + pmmain[b] + "</span></div><div class=\"shadow-box\" id=\"s-box"+b+"\">a</div><BR><BR><img src=\"imgs\\" + pmmain[b] + ".png\"></div>").hide().show(0);}

The m-boxes is where im looking at. Is there a way to get the number of the clicked m-box (eg. There's 16 items in the menu and I clicked on the 2nd one, understandably m-box2 is what i clicked.)? What I want to do is to be able to display another series of menu from that.

Which is like:

$(document) .ready(function(){
$("m-box"?????) .one("click", function(){
$('#arrow-enclosure') .append("<div id=\"highlights-box\"><BR><BR><img src=\"imgs\\"+pmmain[????]+"\"><h1 class=\"line-blank\"><span></span></h1><BR><a href=\"pages\\company name\\company name\\PM\\\">STYLE GUIDE</a> <span>|</span> <a href=\"pages\\company name\\company name\\PM\\Sample Output.pdf\">OLD JOBS PDF</a> <span>|</span> <a href=\"pages\\company name\\company name\\PM\\InDesign Package\">INDD PACKAGE</a> </div>);
});
});

The parts where there's a ????? is the thing that I'm not sure if it's possible to do. Any help would be nice. Thanks!

Selim
  • 197
  • 1
  • 11
  • 1
    possible duplicate of [In jQuery, how to attach events to dynamic html elements?](http://stackoverflow.com/questions/1359018/in-jquery-how-to-attach-events-to-dynamic-html-elements) – Maxime Peloquin Sep 03 '15 at 20:08
  • 1
    And why not enclose your strings inside single quotes and get rid of those horrible escapes ;p – brroshan Sep 03 '15 at 20:16

1 Answers1

1

There's an easy way to get the value of the item clicked, and that's using $(this) in the body of the click handler, like this:

$('.main-box').click(function (){

    // $(this) will give you the jQuery element of whatever was clicked,
    // you can do whatever you like here

    console.log($(this).attr('id'));

});

Check out the jsBin made for this question, where a sample menu is generated that you can click and see the results: http://jsbin.com/pisotefeca/edit?js,console,output

I'm open to any questions!

lintuxvi
  • 127
  • 1
  • 9