My goal is to create an HTML anchor with a unique ID. When the user clicks the anchor, I want the ID to be passed to JavaScript using the onclick HTML attribute. Then, a JavaScript function will read the ID and display the corresponding content in a div. We are utilizing the jQuery library for this task.
This is what I have tried so far:
<a id="MyID1" onclick="var ClickVariable=this.id;return false">1</a>
<a id="MyID2" onclick="var ClickVariable=this.id;return false">2</a>
<script>
var ClickVariable;
var ContentBox = [];
ContentBox[ClickVariable] = "Content for MyID1";
$(ClickVariable).click(function() {
$('.dropdown-menu-content').html(ContentBox);
});
</script>
Unfortunately, the above code does not work as expected. We do have a working alternative solution, but it is not very efficient.
<a id="MyID1">1</a>
<a id="MyID2">2</a>
$('#MyID1').click(function() {
$('.dropdown-menu-content').html('Text 1');
});
$('#MyID2').click(function() {
$('.dropdown-menu-content').html('Text 2');
});
Although the above approach works, it involves a lot of repetition when dealing with a large list of elements.
Here is a link to a jsfiddle demonstrating the repetitive workaround: http://jsfiddle.net/2z7o5hn3/