Moving to next array value failure - javascript

I created a js quiz. The questions are stored in a jquery array. However, clicking on next will not proceed to the next array value. How to fix this?
function nextQuestion() {
submt = true;
$('#explanation').empty();
$('#question').append(quiz[currentquestion]['question']).html();
if (quiz[currentquestion].hasOwnProperty('image') && quiz[
currentquestion]['image'] != "") {
if ($('#question-image').length == 0) {
$(document.createElement('img')).addClass(
'question-image').attr('id',
'question-image').attr('name', 'anscho').attr(
'src', quiz[currentquestion]['image']).attr(
'alt', htmlEncode(quiz[currentquestion][
'question'
])).insertAfter('#question');
} else {
$('#question-image').attr('src', quiz[
currentquestion]['image']).attr('alt',
htmlEncode(quiz[currentquestion]['question'])
);
}
} else {
$('#question-image').remove();
}
addChoices(quiz[currentquestion]['choices']);
setupButtons();
}

Solved it. Thanks to Johnny Mopp for pointing out that currentquestion should be incremented. Thanks!!!

Related

Verify State in Script before Adding Line item (NetSuite)

I am trying to add a line to a Sales Oder when a specific item has been added, but before it does, I need it to verify the state first.
This is where my struggle comes in. I have the code to add the item correctly working, but my state check errors out.
function validateField(type, name, linenumber) {
var shipstate = nlapiGetFieldText()
if (shipstate == 'PA') {
function recalc(type) {
if (type == 'item') {
var itemId = nlapiGetCurrentLineItemValue('item', 'item');
if (itemId == 1658) {
//Insert item
nlapiSelectNewLineItem('item');
nlapiSetCurrentLineItemValue('item', 'item', 1516);
nlapiSetCurrentLineItemValue('item', 'quantity', 1);
nlapiSetCurrentLineItemValue('item', 'amount', '0.24');
nlapiCommitLineItem('item');
}
}
return true;
}
}
return true;
}
Any insight to where my issue is? I think my issue is where I'm storing the state value so it can check the variable.
I would recommend adding your line with a recalc function instead of a validateField function. This is because if you try to add a new line while you are still editing an existing line it will cancel out of your current line. With a recalc function it will wait until you hit the 'Add' button on the existing line before checking the state and adding a new line.
You are on the right track with your state validation, the function you need is nlapiGetFieldValue('shipstate');. I believe the code below should solve your issue.
function recalc(type) {
var shipstate = nlapiGetFieldValue('shipstate');
if (shipstate === 'PA') {
addNewLine(type);
}
return true;
}
function addNewLine(type) {
if (type === 'item') {
var itemId = nlapiGetCurrentLineItemValue('item', 'item');
if (itemId === '1658') {
//Insert item
nlapiSelectNewLineItem('item');
nlapiSetCurrentLineItemValue('item', 'item', '1516');
nlapiSetCurrentLineItemValue('item', 'quantity', '1');
nlapiSetCurrentLineItemValue('item', 'amount', '0.24');
nlapiCommitLineItem('item');
}
}
return true;
}

jQuery click events stopping works

I have a problem. jQuery('#FinishTest').click() works, but it stops when you select all the answers. The error does not appear in the console.
my code
jQuery('#FinishTest').click(function() {
var names = {};
jQuery(':radio').each(function() {
names[jQuery(this).attr('name')] = true;
});
var count = 0;
jQuery.each(names, function() {
count++;
});
if (jQuery(':radio:checked').length > 3) {
if (jQuery(':radio:checked').length < count) {
if (confirm("Henüz cevaplanmamış sorularınız var. Testi bitirmek istediğinizden eminmisiniz ?")) {
finishtest();
} else {
jQuery('#FinishTest').bind('click');
return;
}
}
} else {
alert('Testi bitirebilmek için en az 2 soru cevaplamalısınız.');
}
});
That happen because of the following condition in your code :
if( jQuery(':radio:checked').length < count )
When you select all the answers you will end up by jQuery(':radio:checked').length equals six 6 and the count variable value will be also 6 so the condition will never evaluates to true.
Try to add equal to you condition = so it will be inferior or equal <= :
if( jQuery(':radio:checked').length <= count ){
//Your logic here
}
Hope this helps.
Changed and solved problem.
if (jQuery(':radio:checked').length > 3) {
if (jQuery(':radio:checked').length < count) {
if (confirm("Henüz cevaplanmamış sorularınız var. Testi bitirmek istediğinizden eminmisiniz ?")) {
finishtest();
}
} else {
finishtest();
}
} else {
alert('Testi bitirebilmek için en az 2 soru cevaplamalısınız.');
}
There is no error in your code. Just only condition is wrong in if statement.
I have tested on my browser(chrome) with debug.
enter image description here
I hope it will be help you.

function does not execute when button is pressed a second time

This is the function I'm using.
function beHonest() {
if (document.getElementById("about").style.opacity = "0") {
document.getElementById("about").style.opacity = "1";
} else {
document.getElementById("about").style.opacity = "0";
}
}
When I press the button, it calls the function. When I press the button a second time, the else statement does not seem to execute. Why is this?
Your test is currently written like an assignation:
if (document.getElementById("about").style.opacity = "0")
You must write:
if (document.getElementById("about").style.opacity == "0")
Well ==
if (document.getElementById("about").style.opacity == "0") {
You are setting the value to 0 and checking the object. It is true so you enter on closure and set to 1

Return through all functions

Is it possible to return through multiple functions?
I have a jQuery on click function with a $.each loop in it. In the $.each loop I test for various conditions, and if not met display an alert message and then return. Here is a cut down version of my code:
$(document).on('click', '.add-to-basket, #add-to-basket', function(e) {
var data = {
id: $(this).data('id'),
quantity: 1
};
if($('#quant').length > 0) {
data.quantity = $('#quant').val();
}
var i = 0;
var j = 0;
if($('.product-option').length > 0) {
$('.product-option').each(function(index, element) {
if($(this).is('select')) {
//check to see if this is a required select, and return if a selection has not been made.
if($(this).data("force") == 1 && $(this).val() == 0) {
AlertDialogue($(this).data("title") + " requires a selection before you can add this product to your basket.", "Required Option");
return;
}
data.opts[i++] = $(this).val();
} else if($(this).is('input[type="checkbox"]:checked')) {
data.opts[i++] = $(this).val();
//check to see if this is a required group of checkboxes, and if so at least one has been checked. If not return.
} else if($(this).is('input[type="checkbox"]')) {
if($(this).data("force") == 1 && $('input[name="' + $(this).prop("name") + '"]:checked').length == 0) {
AlertDialogue($(this).data("title") + " requires at least one option to be checked before you can add this product to your basket.", "Required Option");
return;
}
} else if($(this).is('input[type="radio"]:checked')) {
data.opts[i++] = $(this).val();
} else if($(this).is('textarea')) {
//Check to see if this is a required textarea, and if so make sure there is some text in it.
if($(this).data("force") == 1 && $.trim($(this).val()).length == 0) {
AlertDialogue($(this).data("title") + " requires text before you can add this product to your basket.", "Required Option");
return;
}
if($(this).val().length > 0) {
data.text[j].id = $(this).data("id");
data.text[j++].val = $(this).val();
}
}
});
}
//submit product to the cart
});
However the return will only break that loop of the $.each loop, and start the next loop. I would like to not only break the $.each loop, but return from the on click function entirely.
Is this possible?
If so, how can I achieve this?
To exit from $.each you should return false
To exit from event handler function you should use return
As per your requirement you can do little like below,
var break = false;
$('.product-option').each(function(index, element) {
// rest of code
if(condition) {
break = true;
return false; // this will break out of each loop
}
});
if(break) {
return; // return from event handler if break == true;
}
// rest of code
Check out the docs for jQuery.each():
We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same
as a continue statement in a for loop; it will skip immediately to
the next iteration.
Essentially, use return false; instead of just return;.

jQuery if (x == y) not working

So, I have some faux checkboxes (so I could style them) that work with jQuery to act as checked or not checked. There are a number of faux checkboxes in my document, and for each one I have a click function:
var productInterest = [];
productInterest[0] = false;
productInterest[1] = false;
productInterest[2] = false;
// here is one function of the three:
$('#productOne').click(function() {
if (productInterest[0] == false) {
$(this).addClass("checkboxChecked");
productInterest[0] = true;
} else {
$(this).removeClass("checkboxChecked");
productInterest[0] = false;
}
});
The problem seems to be that there is an error in the if statement, because it will check, but not uncheck. In other words it will add the class, but the variable won't change so it still thinks its checked. Anybody have any ideas? Thanks for your help.
UPDATE: So, I need to show you all my code because it works in the way I supplied it (thanks commenters for helping me realize that)... just not in the way its actually being used on my site. so below please find the code in its entirety.
Everything needs to happen in one function, because the UI and data for each checkbox need to be updated at once. So here is the complete function:
$('input[name=silkInterest]').click(function() { // they all have the same name
var silkInterest = [];
silkInterest[0] = false;
silkInterest[1] = false;
silkInterest[2] = false;
if ($(this).is('#silkSilk')) { // function stops working because the .is()
if (silkInterest[0] == false) {
$(this).addClass("checkboxChecked");
silkInterest[0] = true;
} else {
$(this).removeClass("checkboxChecked");
silkInterest[0] = false;
}
alert(silkInterest[0]);
}
if ($(this).is('#silkAlmond')) {
if (silkInterest[1] == false) {
$(this).addClass("checkboxChecked");
silkInterest[1] = true;
} else {
$(this).removeClass("checkboxChecked");
silkInterest[1] = false;
}
}
if ($(this).is('#silkCoconut')) {
if (silkInterest[2] == false) {
$(this).addClass("checkboxChecked");
silkInterest[2] = true;
} else {
$(this).removeClass("checkboxChecked");
silkInterest[2] = false;
}
}
var silkInterestString = silkInterest.toString();
$('input[name=silkInterestAnswer]').val(silkInterestString);
// This last bit puts the code into a hidden field so I can capture it with php.
});
I can't spot the problem in your code, but you can simply use the class you're adding in place of the productInterest array. This lets you condense the code down to a single:
// Condense productOne, productTwo, etc...
$('[id^="product"]').click(function() {
// Condense addClass, removeClass
$(this).toggleClass('checkboxChecked');
});
And to check if one of them is checked:
if ($('#productOne').hasClass('checkboxChecked')) {...}
This'll make sure the UI is always synced to the "data", so if there's other code that's interfering you'll be able to spot it.
Okay, just had a palm to forehead moment. In regards to my revised code- the variables get reset everytime I click. That was the problem. Duh.

Categories

Resources