Improving code for learning purposes [closed] - javascript

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/

Related

Java script doesnt work [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to learn javascript and I found an example but icant make it run What am i doing wrong
function CheckForPastDate(sender, args) {
selectedDate = sender._selectedDate;
var todayDate = new Date();
if (selectedDate.getDateOnly() < todayDate.getDateOnly()) {
sender.selectedDate = todayDate;
sender._textbox.set_Value(sender.selectedDate.format(sender._format));
alert("Wrong date!");
}
}
You made couple of mistakes in your code assuming that the code written is in javascript
Date comparisons need to be like this
if (selectedDate.getTime() < todayDate.getTime()) {
And you cannot format the dates in Javascript.
You can use the API available if you want.
Like the one I used MomentJS
sender._textbox.value = moment(sender.selectedDate).format(sender._format);
See my sample: http://jsfiddle.net/vinodgubbala/6MEG6/

In line error validation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a question and im not quite sure what would be the proper terminology.
I have a text box in a form.
When the use fills out some information i would like to check the text box and if there is an error show the error next to the textbox. An alert would work as will but prefer the in line error.
In case you are using jQuery here is some really basic validation:
$(function () {
$('#form').on('submit', function () {
var $input = $('#textbox');
// Make some validation againts the textbox here
if ('' === $input.text()) {
$input.after('<span class="error">Invalid value.</span>');
return false;
}
return true;
});
});
But as above pointed out, instead of reinventing the wheel look for some existing solution, for example the jquery validate plugin.

Get the value of the array index [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 a ajax response array and I want get the value of there index.
[{"UserLatitude":"33.7543","UserLongitude":"-84.3744"}{"UserLatitude":"22.6962","UserLongitude":"75.8651"},{"UserLatitude":"22.6963","UserLongitude":"75.8654"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"37.7858","UserLongitude":"-122.406"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"22.6962","UserLongitude":"75.8653"},{"UserLatitude":"22.6963","UserLongitude":"75.8654"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"33.7543","UserLongitude":"-84.3745"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"0","UserLongitude":"0"},{"UserLatitude":"33.7543","UserLongitude":"-84.3744"}]
You're missing a comma after the first array object which is causing an error. Otherwise result[0].UserLatitude will work - jsfiddle.
You need to iterate the array and then check those values are matching like
$.each (arr, function (index, value) {
if(value["UserLatitude"] == "33.7543" && value["UserLongitude"] == "-84.3744") {
console.log(index);
return false;
}
});
if matches found, print the index and exit the loop.
Fiddle

Adding an id on to a href jquery [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'm trying to get the id of an radiobutton to be added to href when the link is clicked. I tried searching google for a long time. But maybe i'm missing because it's very basic.
I do have a bit of code written but it doesn't work. Help?
function ProductIDLink() {
var url = "/bbbb/aaaaaaa/" + ('.aaasaff:radio:checked').attr('id');
$("#safad").attr("href", url)
}
Looks like you've just forgotten a $:
... ('.aaasaff:radio:checked').attr('id')
should be
... $('.aaasaff:radio:checked').attr('id')
From what I can see the $ was missing.
function ProductIDLink() {
var url = "/bbbb/aaaaaaa/" + $('.aaasaff:radio:checked').attr('id');
$("#safad").attr("href", url)
}

Correct inputs for this? [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 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/

Categories

Resources