4 Replies - 749 Views - Last Post: 03 September 2013 - 09:09 PM

#1 kaseron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-August 12

Trouble with onclick not calling JavaScript function

Posted 29 August 2013 - 09:18 AM

I was writing a code where the goal was to have a link that asks what Javascript is, and have it call a function in a separate js file that would write the answer into a specific div in the page. When I clicked on the link, nothing happened.

Here is my code:
/* pertinent HTML code */
<div id="links">
	
		<h1>Links to Pages</h1>
		<p class="linksub">
			<a href="Home.html">Home</a><br/>
			<a href="javascript: writeDef();" id="what">What is Javascript?</a><br/>
			<a href="javascript: writeCdata();" id="cdat">Using CDATA</a><br/>
		</p>
		
	</div>
	
	<div id="header">
		<h1>My Javascript Website</h1>
	</div>

	<div id="left">
		<p id="whatis">
		</p>
		<p id="cdata">
		</p>
	</div>
	
	<div id="right"><!--other stuff will go here-->
	</div>


And the respective Javascript:
function writeDef(){
	document.getElementById("whatis").innerHTML="Javascript is a scripting language used to make webpages more interesting.<br/>\
	Structurally, it is very much like a less strict version of C.<br/>I hope this helps.<br/>");
	return false;
}
function writeCdata()
{
	document.getElementById("cdata").innerHTML="The CDATA block is used to signal the XML parser not to parse whatever is inside the block. It is used because \"<\" and \"&\" are illegal\
	characters (\"<\" because it signals a new element, \"&\" because it is interpreted as a part of a character entity, such as <em>&amp;</em>, that does not exist.";
	return false;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with onclick not calling JavaScript function

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: Trouble with onclick not calling JavaScript function

Posted 29 August 2013 - 09:30 AM

look in the Error Console (F12) you should find something there.

Quote

Structurally, it is very much like a less strict version of C.

structurally => the syntax.

except for the syntax, JS (a loosely typed, object oriented, prototypal Language with influences from Functional Programming) is completely different from C.
Was This Post Helpful? 1
  • +
  • -

#3 kaseron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-August 12

Re: Trouble with onclick not calling JavaScript function

Posted 03 September 2013 - 01:12 PM

Thanks for the content help.
Was This Post Helpful? 0
  • +
  • -

#4 JoeSimmons  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 10-August 13

Re: Trouble with onclick not calling JavaScript function

Posted 03 September 2013 - 09:03 PM

In writeDef(), you have an incorrect parenthesis at the end of where it sets the innerHTML.

After you fix that, you'll have another problem: your link hrefs.
If I were you, I'd use the onclick attribute:

<a href="javascript: void(0);" onclick="writeDef();" id="what">What is Javascript?</a>
<br />
<a href="javascript: void(0);" onclick="writeCdata();" id="cdat">Using CDATA</a>

Was This Post Helpful? 1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: Trouble with onclick not calling JavaScript function

Posted 03 September 2013 - 09:09 PM

View PostJoeSimmons, on 04 September 2013 - 06:03 AM, said:

If I were you, I'd use the onclick attribute:

<a href="javascript: void(0);" onclick="writeDef();" id="what">What is Javascript?</a>
<br />
<a href="javascript: void(0);" onclick="writeCdata();" id="cdat">Using CDATA</a>

omit the href and do your link styling with CSS.

or even use neither href nor onclick an assign the event handlers in JS.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1