Results 1 to 2 of 2

Thread: Help me with this function

  1. #1
    Join Date
    Feb 2016
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me with this function

    I have a variable that stores the value of a search box. This value is supposed to represent the id of another element. I want to use the focus command in javascript to focust on the value as an id in the page. how do I do this? This is what I had.

    Code:
    function search(){
    			var val = document.getElementById('search').value;
    			val.focus();
    			}

    Code:
    	<body id="body" onload="document.getElementById('title').focus();">
    	
    
    
    	<input value="  Search" id="search" onclick="document.getElementById('search').value=' '"></input>
    	
    	<button id="button" onclick="search();">Search</button>

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    Trim extra whitespace first, and then getElementById

    Code:
    function trim(str) {
         return str.replace(/^\s+|\s+$/g, '');
         }
    
    function search(){
         var val = document.getElementById('search').value;
         val = trim(val);
         document.getElementById(val).focus();
         }
    Demo: http://jsbin.com/gunacacisa/1/edit?html,js,output
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

Similar Threads

  1. Adding javascript function within ontoggle function
    By piers in forum Dynamic Drive scripts help
    Replies: 6
    Last Post: 07-22-2009, 02:41 AM
  2. Replies: 9
    Last Post: 09-16-2008, 05:31 PM
  3. Replies: 2
    Last Post: 08-02-2007, 06:19 AM
  4. out side of function or inside of function
    By riptide in forum JavaScript
    Replies: 2
    Last Post: 07-12-2007, 04:10 PM
  5. Passing Variables Function to Function
    By jscheuer1 in forum JavaScript
    Replies: 3
    Last Post: 03-26-2005, 12:22 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •