Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created July 3, 2012 01:05
Show Gist options
  • Save ttscoff/3036750 to your computer and use it in GitHub Desktop.
Save ttscoff/3036750 to your computer and use it in GitHub Desktop.
Bookmarklet to select the search field on the current page and focus it
javascript:var%20inputs=document.getElementsByTagName('input'),firstSearch=false,textinputs=[],i,t;for(i=0;i<inputs.length;i++)if(((inputs[i].type==='text')||(inputs[i].type==='search'))&&inputs.disabled!==true)textinputs.push(inputs[i]);for(t=0;t<textinputs.length;t++)if((/search/i).test(textinputs[t].className)||(/(^[sq]$|search|query)/i).test(textinputs[t].id)||(/^(q(uery)?|s|.*?search.*)$/).test(textinputs[t].name)){firstSearch=textinputs[t];break;}if(!firstSearch)textinputs[0].focus();else%20firstSearch.focus();
// searchFocus pre-bookmarkleting
var inputs = document.getElementsByTagName('input'),
firstSearch = false,
textinputs = [],
i,t;
for(i=0;i<inputs.length;i++)
if (((inputs[i].type === 'text') || (inputs[i].type === 'search')) && inputs.disabled !== true) textinputs.push(inputs[i]);
for (t=0;t<textinputs.length;t++)
if ((/search/i).test(textinputs[t].className) || (/(^[sq]$|search|query)/i).test(textinputs[t].id) || (/^(q(uery)?|s|.*?search.*)$/).test(textinputs[t].name)) { firstSearch = textinputs[t]; break;}
if (!firstSearch)
textinputs[0].focus().select();
else
firstSearch.focus().select();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment