Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting clicked element's attributes #135

Open
jelewis8 opened this issue Nov 13, 2020 · 9 comments
Open

Getting clicked element's attributes #135

jelewis8 opened this issue Nov 13, 2020 · 9 comments

Comments

@jelewis8
Copy link

Is there a way to pull the clicked element's attributes (such as custom data attributes, or the href, for instance) into the modaal ?

I've tried $(this) inside before_open but it returns null.

@danodm
Copy link

danodm commented Nov 16, 2020

Hi @jelewis8,

One way to do this is like so:

var my_link = $('.my-link');

my_link.modaal({
    before_open: function() {
        my_link.data('foo');
        // or
        my_link.attr('href');
    },
    after_open: function() {
        my_link.data('bar');
    }
})```

@jelewis8
Copy link
Author

jelewis8 commented Nov 16, 2020 via email

@danodm
Copy link

danodm commented Nov 17, 2020

@jelewis8 Correct. There are a number of ways you could approach this similarly to fetch those attributes. I don't believe this is an issue with Modaal at all, but something for you to establish in the rest of your javascript.

@MikeiLL
Copy link

MikeiLL commented Nov 17, 2020

@danodm that makes sense, but I sure wish you were in the mood to show an example of a solution. : ). Will share whatever I come up with... once I've solved it.

@MikeiLL
Copy link

MikeiLL commented Nov 17, 2020

@jelewis8 Here's what I have come up with

$('.info-popup').on('click', function() {
    var unique_id = $(this).data('unique_id');
    return $(this).modaal({
        after_open: function(ev){
            console.log(unique_id);
        },
    start_open: true
    });
});

What I'm doing is binding the clicked object to the modaal function, using jQuery's on method.

@jelewis8
Copy link
Author

jelewis8 commented Nov 17, 2020 via email

@MikeiLL
Copy link

MikeiLL commented Nov 17, 2020

@jelewis8 I think the cleverness is on behalf of the creators of Javascript (and, I guess jQuery). I think this is actually a really common (unless it's wrong) way of doing it, and you and I are just ignorant (but becoming less so.)

@jelewis8
Copy link
Author

jelewis8 commented Nov 17, 2020 via email

@therbta
Copy link

therbta commented Apr 26, 2022

I've fixed it like this:

<a href="#" class="pop" url="https://google.com"> </a>

$('.pop').on('click', function () {
	var url = $(this).attr('url');
	return $(this).modaal({
		type: 'iframe',
		content_source: url,
	});
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants