You can't pass a string "without quotes", in a JS program text that is not surrounded by quotes is not a string, it is an expression.
Your code doesn't work the way you think it should for a couple of reasons. First, you are not doing anything with the data
parameter that is passed in. .data
is a property of the DOM element you retrieved with getElementById
. data
is the literal name of the property you are accessing, it is not a reference to the value of the data
parameter. In order to access a property with the name that you passed in as the data
variable you will need to use bracket notation instead of dot notation to access it:
function random(data) {
document.getElementById("demo")[data] = "Paragraph changed!";
}
random("textContent");
You'll notice I changed the function name from Random
to random
. This is because it is a convention in JS to only use capitalized names for constructor functions.
The other reason it isn't working is because of your attempt to use a "string" without quotes. When a JavaScript engine sees random(textContent);
it interprets that as meaning call random
passing the value of a variable named textContent
as the first parameter. Since you have not defined a variable named textContent
the value that is passed in is undefined
. Quoting it turns it into a string literal, thus passing in the string you intended to.