Reputation: 1639
I want to call the variable from the script to body.
Here is my script code. I want to take the rightanswers
and wronganswers
values to use in html body.
GetFeedback = function (a) {
if (a == CorrectAnswer) {
rightanswers++;
} else {
wronganswers++;
}
lock = true;
}
Upvotes: 1
Views: 4974
Reputation: 10139
you can use jquery to change the html on the page.
GetFeedback = function(a){
if(a==CorrectAnswer){
rightanswers++;
}else{
wronganswers++;
}
lock=true;
$('p:first').html('Right Answers: '+rightanswers+' <br> Wrong Answers: '+wronganswers);
};
and have this as your html
<body>
<p></p>
<a href="javascript:GetFeedback(a);">Get Feedback</a>
</body>
Upvotes: 0
Reputation: 2776
You need to use the Document Object Modle
. You have different methods with JavaScript to create and insert elements into the DOM. As for example:
var element = document.createElement('p');
var body = document.body;
body.appendChild(element);
With that code you are creating an element, then appendig it into the body. And that is pure JavaScript. You could use Mootools
or jQuery
, and It is goign to be simpler. But JavaScript doesn't work like PHP for example, where you can use the variables mixed up with the HTML.
And if you want to trigger the function from the HTML you need to bind thtat function to an event. For example clicking on a link would be.
HTML
<a href="#" id="button"> Click Here </a>
JS
var b = document.getElementById('button');
b.click = function(){
GetFeedback();
}
Upvotes: 1
Reputation: 56429
Make sure you're declaring the variable (we can't see that in the code provided) by using var
:
<script>
var GetFeedback = function (a) {
if (a == CorrectAnswer) {
rightanswers++;
} else {
wronganswers++;
}
lock = true;
</script>
Then in your HTML, you can use feedback like this (although, it's not good practice to use the below, it's merely for demonstration purposes):
<a href="#" onclick="javascript:GetFeedback();">Hello</a>
Upvotes: 0