if statement incorrect [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 want to have the variable estimatedVal replaced with avgList if estimatedVal is empty. Something is wrong with my code because with the code below nothing happens. If I remove the if statement everything else works:
$('#estimatedVal').html($(context).find('ul.estimates a:eq(1)').text()
+ ' - ' + $(context).find('ul.estimates a:eq(0)').text());
if(estimatedVal == ' - '") {
$('#estimatedVal').html(avgList);
} else {
$('#estimatedVal').html(estimatedVal);
}
What do I need to do to be able to use the if statement?

You've got a "lonely double quote," use an IDE/Code Editor and quit using Notepad :P
if(estimatedVal == ' - ') {
$('#estimatedVal').html(avgList);
} else {
$('#estimatedVal').html(estimatedVal);
}

You've got a stray double quote in your if.

Related

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

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/

Find a string in a string [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
How would I write an if statement that alerts when the var MyString is not in the var URL?
var URL = "http://test.mysite.com/about/";
var MyString = "mysite.com";
if (URL.indexOf(MyString) === -1) {
alert('Not found!');
}
if(URL.indexOf(MyString) == -1) alert("Not found");

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)
}

check if url is a root url [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 looking for a regex to test if a url is a root url:
http://site.org TRUE
http://www.example.com TRUE
...
http://www.example.com/dir FALSE
http://www.example.com/image.jpg FALSE
http://www.example.com/dir/file.doc FALSE
Try below regex will do the work
/^https?\:\/\/[^\/]+\/?$/
I tested like this:
var URL = "http://jsfiddle.net/";
var check =/^https?\:\/\/[^\/]+\/?$/.test(URL);
alert(check);
JSFiddle
try this:
var str='url...';
if(str.indexOf("/")<str.indexOf(".")&&(str.indexOf("http://")>-1))
return true;
else
return fals;
Try out with this regular expression
(?\w+)://(?[\w#][\w.:#]+)/?[\w.?=%&=-#/$,]*

Categories

Resources