0

I am having the following code in my JS.

$('#ytResultsList').append('<li><a href="https://www.youtube.com/watch?v="' + item.id.videoId + '></a></li>'); 

As you can see from the above code, in HTML the result is like below.

<li><a href="https://www.youtube.com/watch?v=" bruvbiwlwfi=""></a></li> 

I want the URL to be like below in HTML.

"https://www.youtube.com/watch?v=bruvbiwlwfi" 

How can I do this in Javascript?

3 Answers 3

2

The issue is because you are formatting your href incorrectly. You need to move your closing " to after the variable like this:

$('#ytResultsList').append('<li><a href="https://www.youtube.com/watch?v=' + item.id.videoId + '"></a></li>'); 
Sign up to request clarification or add additional context in comments.

Comments

1

quotation marks are your problem try:

$('#ytResultsList').append('<li><a href=\'https://www.youtube.com/watch?v=' + item.id.videoId + '\'></a></li>'); 

1 Comment

It too works. But I don't get it easily like the quotation answer above.
1

Templatestrings do have pretty good support in modern browsers and can be polyfilled if you need to support older ones - note the backticks:

const videoId = 'bruvbiwlwfi' const url = `https://www.youtube.com/watch?v=${videoId}` => "https://www.youtube.com/watch?v=bruvbiwlwfi" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.