Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Simple javascript in HTML macro : Confluence

Dan27
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
October 29, 2019

Hello,

I tried to run very simple javascript inside HTML macro in Confluence page.

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

	function myFunction() {
  	document.getElementById("demo").innerHTML = "Paragraph changed.";
	}
</script> </head> <body> <h2>JavaScript in Head</h2> <p id="demo">A Paragraph.</p> <button type="button" id="buttonId" onclick="myFunction()">Try it</button> </body> </html>

 

The button is showed but when I click it, the function didn't work.

Any help?

 

Thanks,

Daniel

1 answer

1 accepted

0 votes
Answer accepted
Jack Nolddor [Sweet Bananas]
Atlassian Partner
October 29, 2019

Hi Daniel,

Could you try to change the order like this?

 

<!DOCTYPE html>
<html>
<head>

</head>
<body>
                <h2>JavaScript in Head</h2>

    <p id="demo">A Paragraph.</p>

    <button type="button" id="buttonId" onclick="myFunction()">Try it</button>

<script type="text/javascript"> function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; }
</script>
</body> </html>

 

 

Regards

Dan27
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
October 30, 2019

Thank you

William Wang
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 28, 2020

Agreed!

The script executed before the component is created.  document.getElementById("demo") will be return undefined

Suggest an answer

Log in or Sign up to answer