views:

46

answers:

3

I am trying to write a bit of jquery to: if there is only one instance of the div which has a class of "option" take the href attribute and redirect to that href, this is what i have to far

<script type="text/javascript">

var count =('Size: ' + $('.option').size());

if (count = 1) {

    $('.readmore a').each(function () {
        var url = $(this).attr('href');
        window.location.replace(url);
    })              

} 
else {

   alert("more than 1");    
}
</script>

It successfully redirects to the correct url, but it doesnt work properly because it if there are more than 1 instance of the div.option then it still redirects and doesnt alert "more that 1" anyone help?

im guessing at one point of the script the count is = to 1 and the jquery beings to does its redirect, but I want it to count all the instances then determine to redirect or not

+1  A: 

You are setting count to 'Size: ' + $('.option').size(), which makes it a string. Then you are comparing it to an int. Also, if (count = 1) should be if (count == 1). Notice the two equal signs.

Rocket
var count = $(".option").size(); if (count == 1){ $('.readmore a').each(function () { var url = $(this).attr('href'); window.location.replace(url); }) }else{alert("more"); }looking better?
AJFMEDIA
+1  A: 

if you are comparing numbers it's better to use === "strict comparison operator"

if ( $('.option').length === 1 ){
        window.location.replace( $('.readmore a').attr('href') );
}
else{
    alert("more than 1");   
}
Bjarki Heiðar
What is the point of an `each` with a `window.locaation.replace`.... the page will be changed after the first one.
Peter Ajtai
Hehe good point I did not refactor that part. will fix it :)
Bjarki Heiðar
+1  A: 

if (count = 1) is always true, since it sets count equal to 1!. if (count == 1) will only be true if count is equal to 1. The two don't have to be of the same type. if (count === 1) will only be true if the two are equal and of the same type.

Count should be how many there are, not a string containing "Size: ...".

You should use the length property instead of .count() since it's slightly faster (this is according to jQuery).

You could wrap the whole thing into a self calling anonymous function, so that you don't pollute the global namespace.... unless you make use of these variables later on (obviously only in the case where you don't redirect the page).

I'm assuming the HTML is before the <script> tags.... if not, you need a doc ready: `$(function() { ... });

<!-- HTML up here ... -->
<script type="text/javascript">
(function() {
var count =($('.option').length);

// "count = 1" SETS COUNT EQUAL TO 1!!!!!
if (count === 1) {

      // Why use each? We are redirecting to the first href.
    $('.readmore a').each(function () {
        var url = $(this).attr('href');
        window.location.replace(url);
    })              

} 
else {

   alert("more than 1... specifically " + count);    
}
})();
</script>

Finally, there is no reason to use .each() since you will redirect after the first .readmore a.... Maybe you should be checking how many .readmore as there are? At any rate this makes more sense to me:

<!-- HTML up here ... -->
<script type="text/javascript">
(function() {
var count =($('.option').length);

// "count = 1" SETS COUNT EQUAL TO 1!!!!!
if (count === 1) {

    var url = $('.readmore a').first().attr('href');
    window.location.replace(url);

} 
else {

   alert("more than 1... specifically " + count);    
}
})();
</script>

jsFiddle example w 2

jsFiddle example w 1

Peter Ajtai
I had done this, and it hadnt worked, but it was because I didnt include the $(document).ready. works fine now thanks
AJFMEDIA
If your `<script>` is after the `div` s there is no reason for doc ready, since it'll only reach the `script` tag after the previous parts of the page are ready..... Like in my 2 jsfiddle examples.... this is what I assumed you were doing.... It's often advantageous to have your JS as far down the page as possible.
Peter Ajtai
@peter, i disagree on your last part `to have your JS as far down the page as possible`. It is best to keep it inside the head tag and use the `$(document).ready` method.. cleaner this way and more maintainable.. have a look at http://stackoverflow.com/questions/1013112/where-should-i-declare-javascript-files-used-in-my-page-in-head-head-or-near
Gaby
@Gaby - It's true that using doc ready in the head tag is cleaner, but it does slow down the loading of the page. I should have been more clear, when I wrote `It's often advantageous to have your JS as far down the page as possible,` I meant it makes your page load faster to have your JS farther down the page, since there is no script in the head tag to be downloaded before all the HTML. This is a technique that's discussed in [Yahoo's suggestions for speeding up the loading of your web page.](http://developer.yahoo.com/performance/rules.html) -- Then you would dl jQuery far down the page too.
Peter Ajtai
@Peter, i know the concept you mention to be true, but you can always use `defer` and keep things tidy .. More of a personal style thingy i guess..
Gaby