I have a jQuery plugin with an if statement in it.
For some strange reason (probably it is just me screwing things up) it always gets in the else part even when the url's are the same.
if (opts.startUrl == track.permalink.url) {
var active = true;
} else {
alert('|'+opts.startUrl+'| |'+track.permalink_url+'|');
var active = false;
}
Check it out # http://dev.upcoming-djs.com
The surrounding code uses track.permalink_url, while the if block evaluates track.permalink.url (which is always undefined), so this condition:
opts.startUrl == track.permalink.url
Always evaluates to false
Update: as #brianpeiris points out, the correct fix here would be to change the condition to:
opts.startUrl == track.permalink_url
Start printing both the values and see what is the difference , otherwise do this
if (opts.startUrl.toLowerCase() == track.permalink.url.toLowerCase())
Related
I'm in a dilemma, I have a search engine which I keep the last results, everything perfect until there.
The problem is that I do not know what to do if I do not have items already saved, ie if it is the first time I search.
if(localStorage.getItem("searchResults") === null) {
// I do not know what to do here ...
}
else {
// Here the code is supposed to do what it has to do
}
Should not I do anything, should I save an empty string, or would I have to change the logic I'm working on?
What are your friends, what are you doing? Thank you
I would have a variable called "noResults", and set it to false, when search results are 0, or you can make it fetch results from server. It's all about context and logic
You could set it to N/A:
if(localStorage.getItem("searchResults") === null) {
localStorage.setItem("searchResults", "N/A");
}
else {
// Here the code is supposed to do what it has to do
}
Or use the following, and when you check if searchResults is null, last search result should be empty.
if(localStorage.getItem("searchResults") !== null) {
// Here the code is supposed to do what it has to do
}
Maybe you don't need an if statement. The if statement provides you with code blocks that can be executed for truthy or falsely expressions.
Instead, use a logical OR || to define a default value.
var results = localStorage.getItem('searchResults') || 'No results...';
I've been working on making a basic little image carousel in jQuery.
Currently at the moment I am stuck on the if else logic inside of my changeImage function.
When the user clicks on the "next" link then the next image in line should fade in. Luckily when I comment out the if else statement I'm able to achieve the images fading out but this is not what I am after. So we know it's a logic issue.
I'm just not sure how to implement the correct syntax with combining conditions within my if else statement and I'm sure this logic could also be much cleaner.
Please review
function changeImage (newIndex) {
var i = newIndex;
var current = i;
// `if` user clicks on next then slide image "right"
// something wrong here with my logic..
if ((newIndex === 'next') && i === (current < lengthOfImages - 1)) {
return current + 1;
}
else {
return 0;
}
// fadeout
listOfImages.fadeOut(transitionSpeed).
eq(i).fadeIn(transitionSpeed);
}
// click function on the next link
$('.next').on('click',function() {
changeImage('next');
});
Some feed back on how to fix this with a few hints towards a solution would be greatly appreciated.
JSFiddle
http://jsfiddle.net/kapena/v82pvq7x/1/
Return statement will exit the function. Anything after it will NOT run. If you want to actually return the number, you need to do it at the end.
I think you actually want to set current and not to return. Also your logic really does not make any sense. Most people would do the check like this:
current++;
if (current >= lengthOfImages) {
current = 0;
}
When 'Next' is clicked, the following is happening:
changeImage is fired, passing in 'next' as its parameter.
Within this function, a variable of i is declared and set as 'next'.
A variable of current is also being set to i, which is currently set to 'next'.
Your if statement checks to see if newIndex(the passed in parameter) is equal to 'next' as well as if i is equal to a boolean of current < lengthOfImages - 1. This is evaluating to a boolean, and i is not a boolean. This is why your function is not firing appropriately.
Your return statements in your conditionals are causing your function to complete, making it so your fadeOut and fadeIn transitions never get a chance to execute.
This part
if ((newIndex === 'next') && i === (current < lengthOfImages - 1))
is always false:
i = 'next'
current = 'next'
(current < lengthOfImages - 1) is a boolean
therefore the === is always false, and flow goes to the return clause.
I'm trying to change the text of a button with the following code.
// hide unavailable courses
$("#availability_button").click(function () {
$(".availability_red").toggle();
if ($(this).val('Show Unavailable')){
$(this).html('Hide Unavailable');
} else {
$(this).html('Show Unavailable');
}
});
The button text changes the first time I use it, but never again. Not sure why that is and I have pretty much hit the limits of my JS debugging knowledge.
I put an alert into it and proved it never reaches down to the else path.
What am i doing wrong?
It always evaluates to true because .val(val) returns the jQuery object and objects are truthy (ToBoolean gives true) values.
Also, you are using .val() whereas you probably want to check the .html()
Try this:
if ($(this).html() === 'Show Unavailable') {
$(this).html('Hide Unavailable');
} else {
$(this).html('Show Unavailable');
}
Demo http://jsfiddle.net/jfetf/
$(this).val("Show Unavailable") is setting the value and returning an object, it's not checking equality.
Try $(this).val() == "Show Unavailable" instead. It will take the current value and compare it to the string.
$(this).val('Show Unavailable') return jQuery object which is interprete as true.
Also $(this).val('Show Unavailable') set value to element...
I've got an two if() statements, for which the conditions are both met with the default values in the <select> and <input> form fields I've tested this by assigning the values to a variable and writing the variable. (0 and Url).
However, it seems that neither if() statement's contents execute properly.
Here's a link to my JSFiddle: http://jsfiddle.net/cfRAk/2/
Any edits/answers as to why this is happening would be greatly appreciated!
Change this line:
var geo_select_val = $('select[name=g_country\\[1\\]]').val();
To this:
var geo_select_val = parseInt($('select[name=g_country\\[1\\]]').val());
The thing is geo_select_val is actually "0" and not 0. Converting a string to boolean will only result in false if string is empty. "0" is not empty, so it was being evaluated as true. Since you are going !geo_select_val, it never goes in.
Caveat: this fix will only work if you make sure all values are numbers. If that's not the case, check for equality with "0"
Here's the code you're asking about:
$('#post-form').click( function() {
var geo_select_val = $('select[name=g_country\\[1\\]]').val();
if(!geo_select_val) {
var geo_url_val = $('input[name=g_url\\[1\\]]').val();
if(geo_url_val != "http://google.com") {
$('#notification').html("You need to enter a valid url");
}
}
});
When I set a breakpoint in this click function and then click on the Post Form div, geo_select_val comes back as "0" which means that if(!geo_select_val) will fail because geo_select_val does have a value so the first if condition will never be executed.
Perhaps you want the first if condition to be:
if (geo_select_val != "0") {
which will tell you if some other value besides the default has been selected (assuming you add other options to that select tag with different values).
I'm checking a number of 'read more' links on my blog, and either hiding the link (for the first two posts), or hiding the content and keeping the link. I'm running the links' id attributes through an if ... else statement that looks like so:
$(document).find('.contentclicker').each(function(){
var pt = $(this).parent().attr('id');
if (pt == "postnum1" || "postnum2"){
$(this).hide();
}
else{
$(this).next().hide();
}
});
Note: There's some jQuery in there, but it's not relevant. I know from debugging that the var pt is being set correctly to post_num_1, post_num_2, etc. - but when it evaluates post_num_3 and so on, it doesn't go to the else. I've tried == and ===, among other things, and I can't figure out what's wrong.
Any suggestions?
I am pretty sure you cannot do if (pt == "postnum1" || "postnum2") in javascript. Try if (pt == "postnum1" || pt == "postnum2"). Basically, even if the first conditional of pt == "postnum1" were false, it'd treat the second conditional as true which would avoid the else clause at the bottom. At least, that's what I think.
Sorry if I misunderstood your question.
JavaScript is not strictly typed, it means in particular that it gives you some leeway int your data types and always tries to coerce the expression value to the data type it thinks to be your intention.
In your if statement it tries co convert the part after || to boolean and result of conversion of "postnum2" is always true.
I think what you intended was (pt == "postnum1" || pt == "postnum2")
The second part of condition "postnum2" always evaluates to true. You have to convert condition to first answer. Also your post says post_num_1, post_num_2, etc, but you are checking for post_num1.
Instead of if (pt == "postnum1" || "postnum2")
try
if ((pt == "postnum1" ) || (pt == "postnum2"))
{
// something
}
Also you can do something in the switch case(as an alternative)
switch(pt)
{
case "postnum1":
case "postnum2" : $(this).hide(); break;
default: $(this).next().hide(); break;
}