user695868
user695868

Reputation:

putting title inside div on click

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

Answers (1)

Roko C. Buljan
Roko C. Buljan

Reputation: 206353

  • Run the expected actions inside the desired function
  • Your 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

Related Questions