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();
};
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();
});
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();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With