Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting javascript variable into html output of .innerHTML =

I am trying to insert a variable passed to my function into the output of my .innerHTML = code but do not know how to properly insert it into the HTML output.

function playsong(song)
{
    parent.document.getElementById('player').innerHTML = '<object width="199" height="26"><param name="movie" value="audio_player_black.swf"><embed src="audio_player_black.swf?audio_file=upload/'[song]'&color=00000" width="199" height="26"></embed></object>';
}

I just get [song] in my HTML output rather than the value of [song]

Not sure how I need to do this properly

like image 918
ian Avatar asked May 27 '09 18:05

ian


People also ask

How do you put a JavaScript variable in HTML?

You cannot use js variables inside html. To add the content of the javascript variable to the html use innerHTML() or create any html tag, add the content of that variable to that created tag and append that tag to the body or any other existing tags in the html.

How do I display JavaScript results in HTML?

JavaScript Display PossibilitiesWriting into an HTML element, using innerHTML . Writing into the HTML output using document.write() . Writing into an alert box, using window.alert() . Writing into the browser console, using console.log() .

How do I add a variable to innerHTML?

var add = aux[i]. split("#"); cell5. innerHTML = "<img src='MatrixLogos/add[3]' width='100' height='25'/>"; but this give add[3] in the html instead of the value inside add[3].

How do I get the value of a variable in HTML?

Use the <var> tag in HTML to add a variable. The HTML <var> tag is used to format text in a document. It can include a variable in a mathematical expression.


1 Answers

easy:

parent.document.getElementById('player').innerHTML = '<object width="199" height="26"><param name="movie" value="audio_player_black.swf"><embed src="audio_player_black.swf?audio_file=upload/'+song+'&color=00`000" width="199" height="26"></embed></object>';

just like concatenating any 2 + strings

like image 105
mkoryak Avatar answered Sep 16 '22 16:09

mkoryak