String and variable with innerhtml

Hi,
How do I get a string and a variable to output with innerhtml?

I have tried,

function changeTextOne(){
var x = 3;
    document.getElementById('boldStuff').innerHTML = 'Shane', x;
}

I can get a variable number to work on its own,

document.getElementById('boldStuff').innerHTML = x;

or a text string to work on its own,

document.getElementById('boldStuff').innerHTML = 'Shane';

but not the two together,

I have tried,

'Shane' x;

'Shane', x;

'Shane' + x;

But none work.
Thanks,
Shane

Show us your ‘Shane’+x; attempt not working :slight_smile: . Please give us a codepen reproducing the problem. Because it works.

http://codepen.io/ryanreese09/pen/GgzNJW

I see now why it wasn’t working. I was using

 x = 3;

instead of

var x = 3;

the code is,

<html>

<script type="text/javascript">
var x = 3;
function changeTextOne(){
    document.getElementById('boldStuff').innerHTML = 'Shane ' + x;
}
function changeTextTwo(){
    document.getElementById('boldStuff').innerHTML = 'Harry ' + x*x;
}
function changeTextThree(){ 
    document.getElementById('boldStuff').innerHTML = 'Matt ' + x*x*x;
}
</script>
<p>Welcome to the site <b id='boldStuff'>dude</b> </p> 
<input type='button' onclick='changeTextOne()' value='Change Text to..'/>
<input type='button' onclick='changeTextTwo()' value='Change Text to something else'/>
<input type='button' onclick='changeTextThree()' value='or if still not happy'/>
</html>

Works now,
Thanks for your help,
Shane

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.