1

Why does this not work?

$( ["blog","user","forum"] ).each(function(num,opt) {
    if ( window.location.pathname.indexOf(opt) != -1 ) {
        $('#rb-' + opt).attr('checked','checked');
        return false;
      }
});

When I input $('#rb-blog').attr('checked','checked'); it works as expected?

console.log(typeof opt) produces string and the expected value.

--- UPDATE ---

I've just seen the html is being written to the page via ajax and is executed on .ready() :( Thanks for the help all, much appreciated.

8
  • 2
    Your code looks just fine. Are you sure window.location.pathname contains the option? Commented Sep 10, 2012 at 14:19
  • 2
    shouldn't it be $("#blog","#user","#forum")? Commented Sep 10, 2012 at 14:19
  • Is your code in a readyor load block? Your checkboxes may not be initialized yet. Commented Sep 10, 2012 at 14:20
  • Like @ComputerArts mentioned, it should be either an id, type or classname - [".blog",".user",".forum"], or #blog etc.. Commented Sep 10, 2012 at 14:23
  • Could you please provide also the html code of the web page? Commented Sep 10, 2012 at 14:25

3 Answers 3

1

What the problem could be if the page is not fully loaded and #rb-blog is not available yet.

$(document).ready(function(){
    $( ["blog","user","forum"] ).each(function(num,opt) {
        if ( window.location.pathname.indexOf(opt) != -1 ) {
            $('#rb-' + opt).attr('checked','checked');
            return false;
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

As already noted in the comments, return false will actually exit the "blog", "user", "forum" loop and thus stop checking the checkboxes once one pathname.indexOf condition is true.

Also you might want to add a console.log(window.location.pathname); to make certain this variable contains what you are checking on. Perhaps it is a casing issue?

If you want to know which literals are present in pathname you could use this:

var isPresent = [];
$( ["blog","user","forum"] ).each(function(num,opt) {
    if ( window.location.pathname.indexOf(opt) != -1 ) {
        $('#rb-' + opt).attr('checked','checked');
        isPresent.push(opt);
    }
});

If you just want to know if one of the literals is present in pathname:

var isAtLeastOneIsPresent = false;
$( ["blog","user","forum"] ).each(function(num,opt) {
    if ( window.location.pathname.indexOf(opt) != -1 ) {
        $('#rb-' + opt).attr('checked','checked');
        isAtLeastOneIsPresent = true;
    }
});

1 Comment

Thx, exiting the loop after the first match is the desired behaviour.
0

Solution: the HTML content was not yet written to the page, so now we wait for ajax requests to complete, then we call the function to update the select value, like so...

// apply checked to the selected element
function setAsChecked() {
    $.each(["blog","user","forum"], function(num,opt){
        if (window.location.pathname.indexOf(opt) != -1) {
            $('#rb-' + opt, '.radio_menu').attr('checked','checked');
            return false;
        }
    });
}

// $('.radio_menu').ajaxStop(function(){
//  setAsChecked();
// });

// UPDATE! ajaxStop() catches all our ajax events - not cool. Instead, use when
$.when( $.ajax("") ).done(function(){
    setAsChecked();
});

Maybe that saves someone else a headache!

--- EDIT ---

BE WARNED! This solution caused a major headache when used with CakePHP. The layout disappeared when we called this function in the footer.

See this thread: CakePHP no layout on back and forward button

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.