0

I would like to ask quite a basic question. I need to find how to send a string value in a function as a parameter without any quotation marks on it. such as. my html file would look like :-

<p id='demo'>
</p>

and js code would be :-

function Random(data){
  document.getElementById("demo").data = "Paragraph changed!";
}

Random(textContent);

but it just doesn't work! So can I use some other technique to get the job done, or is it basically impossible?

slapbot
  • 627
  • 1
  • 6
  • 17
  • 1
    What do you mean when you say it "doesn't work?" What were you expecting to happen, and what actually happened? – user94559 Jul 24 '16 at 10:38
  • try this : `document.getElementById("demo").innerHTML = "Paragraph changed!";` – demo Jul 24 '16 at 10:39
  • My question was general, I wanted to know how to access an attribute of an object using a string variable , and one of the guys answered me down below.. So cheers mate! – slapbot Jul 25 '16 at 05:16

2 Answers2

1

Your code is correct, but if you want to access an attribute of an object using a string variable you will need to do it like this:

document.getElementById("demo")[data] = "Paragraph changed!"
Ibrahim
  • 2,034
  • 15
  • 22
  • 1
    Wow, I did not get that this is what he was asking... – Paul Jul 24 '16 at 11:34
  • Thanks man! i was looking forward about how to access the attribute.. but didn't know much of the terminology.. Cheers mate! – slapbot Jul 25 '16 at 05:16
0

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.

Useless Code
  • 12,123
  • 5
  • 35
  • 40
  • I'm confounded as to why this (and the other answer) were down voted. If something was wrong with either, it would be nice to know what. – Useless Code Jul 25 '16 at 03:03
  • Thank you so much man! that is what I was looking forward to.. Thank you so much for the response! I am actually a beginner here in stack overflow. – slapbot Jul 25 '16 at 05:15
  • I'm glad it helped and welcome to SO :-) If an answer is helpful, [you should](http://stackoverflow.com/help/someone-answers) upvote it (normally [you need 15 reputation points](http://stackoverflow.com/help/privileges/vote-up) to upvote, which shouldn't be hard to get if you keep participating here on SO; I'm not sure if you can upvote answers on your own question without 15 rep or not). You should also mark the most helpful answer as [accepted](http://stackoverflow.com/help/accepted-answer). – Useless Code Jul 25 '16 at 17:32
  • man! I wanna upvote or mark it as most helpful, but I can't do either of them.. I ain't got 15 rep yet.. but once i get it, I'll come back to upvote.. I wonder who downvoted it tho.. your answer was absolutely perfect for me! Cheers! – slapbot Jul 26 '16 at 14:07