Reputation: 2892
I'm trying to get a button to create text using knockout.js I'm not sure whether its the html or javascript that's having the problem, but what the console is saying is my onclick for the 'start' id is null.
<div style="margin:0 20px 0 20px;" >
<p data-bind="text: currentQuestion"></p>
<form id="discoutQuestions" action="none">
<button id="start" value="start">start</button>
<label>Answer:</label>
<input type="text" id="answer"/>
<button id="answerSubmit" value="submit" onclick="questionare()">submit</button>
</form>
</div>
document.getElementById('start').onClick(ko.applyBindings(questionList));
document.getElementById('answerSubmit').onClick(function questionare()
{
var correct=0;
var count=0;
var questionList= new questionViewModel();
function next()
{
questionList.setQuestion(questionList.getNext());
ko.applyBindings(questionList);
}
var answer= document.getElementById('answer').value;
if(answer==questionList.answers[0][1]&&count!=questionList.getLength())
{
correct++;
count++;
next();
}
else if(answer!=questionList.answers[0][1]&&count!=questionList.getLength())
{
count++;
next();
}
else
{
react= new message();
if(correct/count>.75)
{
react.setQuestion("Congradualtions! You have won a discount.");
}
else{
react.setQuestion("We are sorry, you did not answer enough questions right ofr a discount.");
}
ko.applyBindings(react);
}
});
Additionally, my form tag won't take action="none" and it's not the onclick that's a problem, it's the getElementById.
Upvotes: 0
Views: 1383
Reputation: 44
That's the problem when people who doesn't know javascript programming learns a really tiny bit of programing on jQuery.
If you want your code to work, read about event listeners or direct event assignation, in this case you need to assign the function to the handler, like this:
document.getElementById('start').onClick = ko.applyBindings;
But there's the catch, when you try to assign an event handler like this, you can't send any parameter, and the only parameter received is the event object that is generated magically when an event is triggered
Upvotes: -2
Reputation: 17366
You can do like this:
document.getElementById('answerSubmit').onclick = function questionare(){}
Or
further go like this:
<button id="answerSubmit" value="submit" onclick="questionare()">submit</button>
function questionare(){
..............Rest of code.........
}
Upvotes: 0