0

What I am trying to do:

dynamically build a onclick event attached to an img tag and pass 3 values. The problem is that the onclick is within a loop so the value of the passed values change depending on their position within the loop.

I have managed to get one value passed by attaching it to the img id, but im at a loss as to how to pass the other two

newElement.innerHTML = '<img id="'+ id + '" class="Videoimg" onclick="myFunction(this.id,\"" + VideoTitlePlayer + "\","' + VideodescPlayer + '")" src="' + thumbnail +'"><div id="VideoNames" class="VideoTitle">'+ title +'<br><div class="VideoDescription">' + desc + '</div></div>';

OK so this is the issue
This function is called to build a menu of youtube videos

$.each(playlistVideoItems, function (index, item) 
            {
            displayVideoResult(item.snippet, item.snippet.resourceId.videoId);
            });

function displayVideoResult(videoSnippet, VideoPlay) 
{ var title = videoSnippet.title;
var desc = videoSnippet.description;
var thumbnail = videoSnippet.thumbnails.default.url;
var id = VideoPlay;

var newElement = document.createElement("div");
newElement.setAttribute("class", "VideoContainerCSS");
    // Set the value to what you want
  newElement.innerHTML = '<img id="'+ id + '" class="Videoimg" onclick="myFunction(this.id)" src="' + thumbnail +'"><div id="VideoNames" class="VideoTitle">'+ title +'<br><div class="VideoDescription">' + desc + '</div></div>';

document.getElementById("results").appendChild(newElement);
}

I need some way to build into the link the videoID, video Title and Video desc so when the img link is clicked it passes all 3 to a function that runs the youtube api

1
  • Maybe you should post the entire loop, so we can understand what you're talking about ? Commented Sep 4, 2017 at 21:06

2 Answers 2

1

Since you have jQuery, you can avoid using onclick and use a jQuery selector and event handler.

Once you have created the elements, use a jQuery selector to capture them. Better yet, use one on the container that uses a delegated event style.

You might need to change the markup just a bit to get the strings, though.

$('.container').on('click', 'img', function(e) {
  var $el = $(this);
  myFunction(this.id, $el.find('.VideoTitle').text(), $el.find('. VideoDescription').text());
});

Using this format you can create one event handler that supports all your elements.

Sign up to request clarification or add additional context in comments.

2 Comments

jquery isnt my first language lol but how will this allow me to build the menu with each title and desc?
@SeamusClarke This doesn't build the menu. That still happens separately. I'll update my answer to provide more detail
0

just use data-id, data-video-title, data-other-prop etc. then onclick="myFunction(this.dataset, ...

2 Comments

tried this but when this.data-video-title and data-other-prop are referenced in the onclick its not firing,
completly my fault I was trying to pass the values, took your soluction and instead of passing this.dataset I just passed the whole object onclick="myFunction(this.dataset)" and works a treat, Thanks Alot Seamus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.