phpmeh
phpmeh

Reputation: 1792

Turning .innerHTML or .value into a variable

I am trying to turn the .innerHTML or .value part of document.getElementById( id ).innerHTML into a variable.

<script language="javascript">
function changeTheText(){
    var newValue = "innerHTML";
    document.getElementById( "myJavaTest" ).newValue = "world";
    alert( newValue );
}
</script>
<span onclick="changeTheText()" id="myJavaTest">Hellow</span>

It isn't throwing an error, but it is also not working. The alert is popping fine.

Is this possible?

Upvotes: 1

Views: 144

Answers (2)

MaxiWheat
MaxiWheat

Reputation: 6251

Use the square brackets, which can be used to access objects properties.

document.getElementById( "myJavaTest" )[newValue] = "world";

Have a look at this question, it might help :

JavaScript property access: dot notation vs. brackets?

Upvotes: 1

user2437417
user2437417

Reputation:

Square brackets instead of a dot.

document.getElementById( "myJavaTest" )[newValue] = "world";

Upvotes: 4

Related Questions