0

I'm trying to add a variable within a new variable.

My first variable is:

var video5 = myObj.find('hosted-video-url').text(); (this returns a direct link to an mp4-file)

My second one should be something like:

var playvid5 = "playVideo('"video5"')";

Variable playvid5 should result "playVideo('http://link.to/video.mp4)')"

When I try to make variable playvid5 in the way I showed above, my whole code stops working and nothing is displayed. When I use var playvid5 = "playVideo('"+video5+"')";, the output is "playVideo('')", so that's not what I need either.

I'm trying to place the 2nd variable in this piece: ('<a href="#" onclick="'+playvid5+'">Bekijk video</a>')

In what way can I place the first variable in the second one?

2
  • Sytactically, var playvid5 = "playVideo('"+video5+"')"; is entirely correct. The fact that your're ending up with "playVideo('')" indicates that video5 is not being set properly, ie video5 is null. Commented Mar 29, 2012 at 14:35
  • It would be helpful if you would post the HTML for the relevant elements. Commented Mar 29, 2012 at 14:37

3 Answers 3

1

Try to replace video5 string by its value.

var video5 = myObj.find('hosted-video-url').text();
var playvid5 = "playVideo('video5')";
playvid5 = playvid5.replace("video5", video5);
Sign up to request clarification or add additional context in comments.

1 Comment

Hi ShankarSangoli Thanks for your answer, this fixed my problem! I'm a beginning Jquery/Javascript coder, so I never heard of the replace function!
1

Why not just give the <a> tag an "id" value, drop it in the document, and then do:

$('#whatever').click(function() { playVideo( video5 ); });

Now, where you go to find the value, I don't think you've got the correct selector. Probably you need

var video5 = myObj.find('.hosted-video-url').text();

The "." before the string "hosted-video-url" is to select by class name. If "hosted-video-url" is an "id" and not a class, then you don't need to use .find(); you can select by "id" with $('#hosted-video-url').

4 Comments

What if OP is looking in an xml document? It can be an xml node also.
What difference would that make? edit Oh wait, I see; you mean that "hosted-video-url" could be a tag name? Yes, that's possible; if true I would be disappointed that the OP did not point out that important fact, and that "xml" wasn't included in the tags for the question.
It's because I am parsing an XML file and can therefore not give an id to the a-tag. Or maybe I can, but it's not necessary, because ShankarSangoli's answer fixed it :) Thanks for your answer though! Edit: I see your reaction now and I'm sorry I did not add that I'm parsing from XML. Didn't think it was of any use for you to know.
Thanks for your update, and I'm glad your code works now! Don't worry about me, but you'll help yourself get more accurate answers if you try to include important details in your question.
1

Do you mean

var playvid5 = "playVideo('" + video5 + "')";

playvid5 will then be the string "playVideo('http://whatevervideo5is')

if video5 is blank then you will get "playVideo('')" so maybe that is the issue.

2 Comments

As I said (and if you read my post carefully instead of just answering really fast) that returns "playVideo('')"
@Rvervuurt If that's true, then you would get exactly the same result from Shankar Sangoli's answer; in other words, if the "video5" variable contains the empty string, then it the other answer won't help you.

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.