views:

145

answers:

7

I have a function which I need to appear within a jQuery $(document).ready(function() {} - I am au fait with javascript but not really worked with jQuery. How can I jQuerify this function?

    function populateContext()
{
    contextTxtBox = document.getElementById('searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.value = pathArr[1].toUpperCase(); 
};
+3  A: 

It's actually nearly identical since the only thing I find worth jQuerifying (nice word) is the DOM element.

function populateContext()
{
    var contextTxtBox = $('#searchContext');
    var pathArr = window.location.pathname.split('/');
    contentTxtBox.val(pathArr[1].toUppercase());
}

$(document).ready(function()
{
    populateContext();
});
BoltClock
contentTxtBox.value does it works too?
Fincha
That was a typo, I meant `val()`.
BoltClock
cane be simplified to $(document).ready(populateContext) if this all you're doing
Juan Mendes
can be simplified further to `$(populateContext);` if this all you're doing
David Murdoch
+1  A: 

this way, if i understand you correctly

function populateContext()
{
    contextTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.val(pathArr[1].toUpperCase()); 
};
Fincha
+1  A: 
$(document).ready(function() {
//whatever code you want

});

function populateContext()  {
    pathArr = window.location.pathname.split( '/' );
    $("#searchContext").Val(pathArr[1].toUpperCase()); 
};

just a sidenote: jQuery IS javascript so you can mix and match :)

Stefanvds
A: 
function populateContext(){
    contentTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    $(contextTxtBox).val(pathArr[1].toUpperCase());
}
Rocket
A: 

I am not sure aout what you mean, but you could just do:

$(document).ready(function() {
    populateContext()
});

If you want to improve your function to make use of jQuery, you could do this way:

function populateContext() {
    var $contextTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    $contextTxtBox.val(pathArr[1].toUpperCase()); 
};

If you provide more information, about what exactly is your doubt, i may be able to explain better.

Uoli
My actual instructions are to add: on DOM loaded function listener 'DomLoaded' -----------function populateContext(){ contextTxtBox = document.getElementById('searchContext'); pathArr = window.location.pathname.split( '/' ); contextTxtBox.value = pathArr[1].toUpperCase(); }// populate the search contextDomLoaded.load(populateContext);How does Jquery handle listeners??
ashash
It depends on the kind of event you are interested, in your case, is the DOM Ready event, you can simply: $(document).ready(callback); and the jQuery will call the "callback" function when DOM is ready. But i think you should read: http://api.jquery.com/ready/ it will make things more clear :)
Uoli
+1  A: 

This should do the trick

function populateContext() {
    var aPath = $( location ).attr( 'href' ).split( "/" );
    $( '#searchContext' ).val( aPath[1].toUpperCase() )
}
michael
I seem to be the only one who is jQuerifying the location :)
michael
Nice - cheers chaps much appreciated as always.
ashash
+1 for jQuerifying the location
Rocket
+3  A: 

jQuerify? Make it a plugin!

(function($){

    $.fn.populateContext = function(){
        var pathArr = window.location.pathname.split( '/' );
        return this.val(pathArr[1].toUpperCase());
    };


}(jQuery));

and use it like this

$(document).ready(function(){
    // Same as window.onload
    $("#searchContext").populateContext();
});
Epeli
Shucks, I just used up my last vote elsewhere so I can't up this one. But props for turning it into a plugin! True spirit of jQuery :)
BoltClock
now that's really jQueryfied haahahaha
mcgrailm