0

I'm trying to take an a element and take the information from the data-id attribute and store it into a variable. I want to then splice this variable so I get the part that I need and implement it into another variable with some text. From that I then want to replace the src attribute of an iframe with the variable. Unfortunately, this doesn't seem to be working at all and I can't find the issue.

Here is the code:

$('.watchvideo').click(function(){
    var idu = $(this).attr('data-id');
    var id = "//www.youtube.com/embed/"idu.substr(27,37);

    $('.videofeatem').setAttribute("src", id);

});
2
  • if you add a jsfiddle it would be easier to debug, but from the code it looks like you need var id = "youtube.com/embed/"+idu.substr(27,37); Commented Jan 6, 2015 at 19:00
  • 1
    var id = "//www.youtube.com/embed/" + idu.substr(27,37); Check if this works. Seems like you missed a +. Commented Jan 6, 2015 at 19:00

4 Answers 4

3

You have 2 issues in code:

1) concatenating substring

2) setting attribute via jquery

var id = "//www.youtube.com/embed/"+idu.substr(27,37);
$('.videofeatem').attr("src", id);
Sign up to request clarification or add additional context in comments.

1 Comment

This answer maybe covers all the problems.
2

Without seeing the HTML, it's tough to be sure, but a + fixes the obvious problem:

$('.watchvideo').click(function(){
    var idu = $(this).data('id');
    var id = "//www.youtube.com/embed/" + idu.substr(27,37);

    $('.videofeatem').attr("src", id);
 });

Also, note that data-xxx attributes can be read by jQuery as .data('xxx')

Comments

0

Simply.

var id = "//www.youtube.com/embed/" + idu.substr(27,37);

Comments

0

Since you're using jQuery, use the .attr() method instead of the .setAttribute().

Comments

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.