Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery v javascript

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(); 
};
like image 999
ashash Avatar asked Sep 08 '10 15:09

ashash


2 Answers

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();
});
like image 125
Epeli Avatar answered Oct 12 '22 11:10

Epeli


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();
});
like image 21
BoltClock Avatar answered Oct 12 '22 11:10

BoltClock