Reputation:
Currently using:
$(function () {
$(".playbtn").on("button", "click", function (e) {
var title = $(this).attr('name');
});
});
$('.movie-title').html(title);
I am trying to get the content of name for playbtn on click and store it in a variable. Then put this content into a div.
Upvotes: 1
Views: 67
Reputation: 206353
delegated element selector
is in the wrong place. Should be
$(function () {
$(".playbtn").on("click", "button", function() {
var title = $(this).attr('name');
$('.movie-title').html(title);
});
});
IF you want to use that variable inside another function you need to make a global use of your var
like:
$(function () {
var title = ''; // DEFINED
$(".playbtn").on("click", "button", function() {
title = $(this).attr('name'); // SET
$('.movie-title').html(title); // USED
});
$("#alertTitleButton").on("click", function() {
alert( title ); // USED
});
});
Read more: jquery.com/on
Post Scriptum:
Be aware that the above will target all elements .movie-title
so be more specific using your jQuery selectors:
jquery.com/selectors jquery.com/traversing
Upvotes: 2