HazzaOb
HazzaOb

Reputation: 71

Selecting an element that has just been clicked JQuery

I have a <div> containing a <a> tags and further divs:

        <div>
            <div class="icons">
                <div class="group popOutDisplay">
                    <a href="#" data-id="128910"><i class="fa fa-home"></i></a>
                    <a href="#" data-id="239019"><i class="fa fa-search"></i></a>
                    <a href="#" data-id="346653"><i class="fas fa-bicycle"></i></a>
                </div>
                <div class="group">
                    <a href="#"><i class="fa fa-globe"></i></a>
                </div>
                <div class="group bottom">
                    <a href="#"><i class="fa fa-trash"></i></a>
                </div>
            </div>
            <div class="content">
                <div id="128910";>
                    <p>some content</p>
                </div>
                <div id="239019";>
                </div>
                <div id="346653";>
                </div>
            </div>
       </div>

Im trying to select the data attribute on the anchor tag with jquery so that i can display the the <div> tags in <div class="content"> which have the same ID (if that makes sense?).

So far i've been able to identify the data id with

$(".group.popOutDisplay a")[0].attributes[1].value;

but this only gives the ID of the first element, due to the [0] index.

tldr: How can I get the data-ID of the <a> tag that has just been clicked?

Upvotes: 3

Views: 68

Answers (5)

Tiramonium
Tiramonium

Reputation: 867

$(document).on("click", "[data-id]", function (event){
    event.preventDefault();
    var id = $(this).attr("data-id");

    // Apply your logic here
    // $("#" + id)
});

Upvotes: 0

Radonirina Maminiaina
Radonirina Maminiaina

Reputation: 7004

Here we go:

$('.popOutDisplay a').click(function(e) {
    e.preventDefault();
    console.log($(this).data('id'));
});

Here the documentation of .data() attribute.

Demo: https://jsfiddle.net/ru4pjz3c/2/

Upvotes: 3

Sumesh TG
Sumesh TG

Reputation: 2575

$(".group a").click(function(){
     var data=$(this).attr("data-id");
     alert(data)
});

To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.

Upvotes: 0

Harrison Ifeanyichukwu
Harrison Ifeanyichukwu

Reputation: 292

bind click event listener on the a element container div <div class="group popOutDisplay". the reason is because click events on the a elements will bubble up to div container.

something like

$('.group.popOutDisplay').bind('click', function(e) {
    var target = e.target;
    if (target.tagName.toLowerCase() === 'a') {
        e.preventDefault(); //prevent the default action of navigating to the url
        var id = target.dataset.id;
        //you now have the id;
        $('#' + id).css('display', 'block'); //display the div
    }
});

Upvotes: -1

Super User
Super User

Reputation: 9642

You can use ".click()" for this. check snippet below..

$('.popOutDisplay a').click(function(e){
    e.preventDefault();
    var elID = $(this).data('id');
    console.log(elID);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<div>
            <div class="icons">
                <div class="group popOutDisplay">
                    <a href="#" data-id="128910"><i class="fa fa-home"></i></a>
                    <a href="#" data-id="239019"><i class="fa fa-search"></i></a>
                    <a href="#" data-id="346653"><i class="fas fa-bicycle"></i></a>
                </div>
                <div class="group">
                    <a href="#"><i class="fa fa-globe"></i></a>
                </div>
                <div class="group bottom">
                    <a href="#"><i class="fa fa-trash"></i></a>
                </div>
            </div>
            <div class="content">
                <div id="128910";>
                    <p>some content</p>
                </div>
                <div id="239019";>
                </div>
                <div id="346653";>
                </div>
            </div>
       </div>

Upvotes: 2

Related Questions