-4

im trying to change a line of text if a link is clicked, however it doesnt seem to be calling my script to change it.

<script >
         var paragraphToChange = document.getElementById("q1");
         paragraphToChange.innerHTML ="Quotation is by:" + <a href="http://www.quotationspage.com/quote/1463.html">Antole France</a>

</script>


 <div id="body">
       <p>javascript.html - JavaScript page</p>
       <a href id="q1">Quotaion is by:</a>
 </div>
  • you have to do this after the html has been loaded. and where is your click event? – Daniel A. White Feb 12 '15 at 18:56
  • There is so much wrong with this I don't know where to begin. How about: Why do you think this should do anything at all in response to something being clicked? (You also really, really need to open your browsers developer tools and read the error messages on the console) – Quentin Feb 12 '15 at 18:57
  • I'm pretty new to this lol, its not showing anything.. im coding it in n++ – Andrew Barsoom Feb 12 '15 at 18:59
  • Start by studying the simple example given in: http://www.w3schools.com/jsref/event_onclick.asp – Igor Feb 12 '15 at 19:06

3 Answers3

0

Sir you have to set the value of "newValue" to the quotation.

    var newValue = '"Quotation is by:" + <a href="http://www.quotationspage.com/quote/1463.html">Antole France</a>'
    paragraphToChange.innerHTML = newValue;
Hassan Al Bourji
  • 183
  • 1
  • 13
  • Nope, developer tools returns : Uncaught typeError: cannot set property 'innerHTML' of null in the ' paragraphToChange.innerHTML = newValue;' line – Andrew Barsoom Feb 12 '15 at 19:03
0

You are missing the click event on the Html tag. Hence, this paragraphToChange.innerHTML ="Quotation is by:" + <a href="http://www.quotationspage.com/quote/1463.html">Antole France</a>" never gets call.

Why don't you start with this example first:

<p id="display"></p>

<button onclick="displayBob">Bob</button>
<button onclick="displayTom">Tom</button>

<script>
displayBob = function ()
{
document.getElementById("display").innerHTML = "Hello Bob";
}

displayTom = function ()
{ 
document.getElementById("display").innerHTML = "Hello Tom";
}
</script>
namhoaingo
  • 83
  • 7
0

What you're trying to accomplish is probably something like this:

HTML

<div id="q1">
<a onClick="javascript:clickFunction()">Quotation is by</a>
</div>

JavaScript

clickFunction = function() {
    document.getElementById("q1").innerHTML = "<a href='http://www.quotationspage.com/quote/1463.html'>Antole France</a>";
}

JSFiddle

ksbg
  • 2,916
  • 1
  • 19
  • 29