2

I been stuck with this problem for a while.

All I need is to put a link inside an array that will call a function to change some text.

<!DOCTYPE html>
<html>
<body>

<p id="demo">The link</p>
<p id="demo1">This content should be be changed when I click the text above</p>

<script>

var text = ["<a href=javascript:Test(demo1, milk)>Click me to change text below to milk</a>"];
document.getElementById("demo").innerHTML = text[0];

function Test(id, content){
var container = document.getElementById(id);
container.innerHTML = content;
}
</script>

</body>
</html>

This works if the link is placed outside an array, but inside an array it doesn't.

Any pointers on how I can get this to work? Been trying for 8 hours now. Thanks guys I would really appreciate your assistance.

1 Answer 1

2

You missed the quotes, the arguments should be wrapped.

var text = ["<a href=javascript:Test(demo1,milk)>Click me to change text below to milk</a>"];

should be

var text = ["<a href=javascript:Test('demo1','milk')>Click me to change text below to milk</a>"];

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the input, let me check it out and see if it works. :)
Alright! It's working. Thanks for the input. Been at this for 12 hours and have been looking into alternative approaches like eval. Didn't know it was just the quotation marks. Again thanks. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.