Correct inputs for this? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to take a survey you can say: I am asking a question if they work out a lot or not, then I have different questions for them if they do or don't work out a lot. And then I am asking them if they are female or male. My goal is to give them feedback based on all three things, sex,work out or not, and how they answer questions.
Currently all of my questions go to the same inputs, for the 2 sets of questions. My code is to long to post so I will add a fiddle below .
$('.myOptions').change(function () {
$('.list').removeClass('active');
$('.' + this.value).addClass('active');
});
http://jsfiddle.net/7xM2f/12/

Your braces are not properly nested. For example you have something like
if (tt == 'Male') {
//some code
if (tt == 'Female') {
You are missing the closing brace for the Male block.
There are several such errors. You should properly indent your code to make these easier to track and pay attention to the error console.
http://jsfiddle.net/ExplosionPIlls/7xM2f/15/

Related

How to set variable of image src and use in javascript function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
So if i have some codes like this
If(sceneA){document.getElementById("divA").src="A.png";}
If(document.getElementById("divC")=="A.png"){document.getElementById("divB").src="B.png";}//And alot of codes here
How can I use a var to shorten strings like
document.getElementById("divA").src="A.png"
document.getElementById("divB").src="B.png"
so i can use them both inside (IF) and {function}
As they have to repeat quite a few times in the codes.
Thanks alot!
That's exactly what functions are for.
Define a function for repeating blocks of code as follows:
function setSrc(divName, imgName){
document.getElementById("div"+divName).src = imgName+".png";
}
function srcEquals(divName, imgName){
return (document.getElementById("div"+divName).src == imgName+".png");
}
Then you can replace your code with this:
if(sceneA){setSrc("A","A");}
if(srcEquals("C", "A")){setSrc("B","B");}

How to ask in Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to write a line of code which asks a question and the user has to enter a number. The number is then stored as a variable and used throughout the rest of the script. Such as:
fit1 = "What is your fitness level?"
level = the number the user had entered
something like that. Can't really explain it properly
P.S. I'm writing my code out in gedit because thats what my uni uses.
You can use a prompt like shown below:
var n = prompt("Fitness level");
console.log(n);
window.level = window.prompt('What is my fitness level?');
console.log(window.level); //save in glonal space of window
You can use the Prompt command, and cast the string answer to type number :
var number = parseInt(prompt('Give me a number', '0'));
alert('You said: ' + number);

Javascript logic statement - I got 99 problems [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
...but a glitch ain't one.
Hey there, I am still learning so I apologize for the simple question. I'm just trying out a hypothetical logic statement that would still make sense using javascript.
Example:
if (problems == 99) {
glitch != 1
}
Basically, I'd like it to mean "If I have 99 problems, a glitch isn't one of them" in the shortest code I can muster. Any help is really appreciated!!
Even shorter
glitch = problems !== 99;
Here's a more lyrical version:
if (problems.length === 99 && problems.indexOf("glitch")=== -1){
return "HOVA";
}
You are not setting glitch. Try glitch = false instead

Improving code for learning purposes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have three tabs and I want to navigate when I click on each one of them. The code that I wrote is working just fine, but I believe that is bad coding, there is any way to improve this is just for learning purposes. Thanks!!!!
jQuery(".nuestra_actualidad li:eq(0)").click(function() {
jQuery("#tabs-actualidad").css("display","block");
jQuery("#tabs-articulos").css("display","none");
jQuery("#tabs-noticias").css("display","none");
});
jQuery(".nuestra_actualidad li:eq(1)").click(function() {
jQuery("#tabs-actualidad").css("display","none");
jQuery("#tabs-articulos").css("display","block");
jQuery("#tabs-noticias").css("display","none");
});
jQuery(".nuestra_actualidad li:eq(2)").click(function() {
jQuery("#tabs-actualidad").css("display","none");
jQuery("#tabs-articulos").css("display","none");
jQuery("#tabs-noticias").css("display","block");
});
Replacing jQuery with $ if possible (unless it clashes with another library) and then it can be reduced to a single function by utilising the index of the clicked element and calling the toggle function:
$(".nuestra_actualidad li").click(function() {
var index = $(this).index();
$("#tabs-actualidad").toggle(index === 0);
$("#tabs-articulos").toggle(index === 1);
$("#tabs-noticias").toggle(index === 2);
});
Example - http://jsfiddle.net/gSKeL/

Validation on special codes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special ID in the format of A000000004..A000000150 etc and OD000000001..OD000000150. I have read the forums here and tried out the solutions (many in the form of /^([A]{1}\[0-9]{9})$/ etc.) but none of them have helped.
I hope I'm understanding your question properly, but how about this regex?
/^(A|OD)[0-9]{9}$/
To check a string against this regex, you'd use something like this:
var regex = /^(A|OD)[0-9]{9}$/;
if (regex.test(myCode)) {
// ... do something ...
}
***So the final solution to my question for people who will refer in future:
The code will be:
var regex= /^(A|OD)[0-9]{9}$/;
var myCode=document.forms["formname"]["fieldname"].value;
if(!regex.test(myCode))
{
alert(...);
return false;
}

Categories

Resources