LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2007, 01:22 PM   #1
johngreenwood
Member
 
Registered: Nov 2006
Location: Lancashire, United Kingdom
Distribution: Slackware 13
Posts: 243

Rep: Reputation: 31
XHTML Strict and input field focus


OK, I know this is not programming, but I couldn't think of where else to put this. I am building myself a custom homepage to learn XHTML and CSS. I am trying to to make my Google search box automatically gain focus when the page loads.

I tried this:
Code:
<body onload="document.g.q.focus()">
<form method="get" name="g" action="http://www.google.com/search">
<p><input type="text" name="q" size="31" maxlength="255" value="" />
<input type="submit" value="Google Search" /></p></form>
This worked, but when I put it through the W3C validator, it failed because the "name" attribute is no longer used in XHTML 1.0 Strict.

I also tried to change the name="g" to id="g", as id is supposedly the replacement for name, but it didn't work.

I also tried to use getElementByID(), but if I change name="q" to id="q", the google search doesn't work, and leaves me at Google.com.

Another thing I tries was to give them both the name and id attributes, which also didn't work.

Curiously, if I have "name" in the <form> element, validation fails as name is deprecated in XHTML 1.0 Strict, but "name in the <input> element passes no problem.
 
Old 04-08-2007, 04:41 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
How about this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>TEST</title>
  </head>
  <body onload="document.getElementById('q').focus()">
    <form method="get" action="http://www.google.com/search">
      <p>
        <input type="text" id="q" name="q" size="31" maxlength="255" value="" />
        <input type="submit" value="Google Search" />
      </p>
    </form>
  </body>
</html>
OK, so it's not XHTML 1.0 Strict, but it's valid XHTML 1.1 -- which seems better in my opinion, but maybe not what you're going for.

Works right and validates, too.
 
Old 04-08-2007, 06:53 PM   #3
johngreenwood
Member
 
Registered: Nov 2006
Location: Lancashire, United Kingdom
Distribution: Slackware 13
Posts: 243

Original Poster
Rep: Reputation: 31
Excellent, thanks very much taylor_venable, it works perfectly.
Now I just need to fill up my popup menus with loads of links.

Thank you again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
flash animation text input field shows nothing kyuso Linux - Software 1 09-19-2006 10:17 AM
Control of input focus with dual screens jkjellman Linux - General 0 12-04-2005 01:57 PM
XHTML strict coding is good but how many browsers support it fully? vharishankar Programming 10 07-06-2005 10:28 AM
Unable to set focus to the user name field in login screen using keyboard alone Lindaramachandr Linux - General 3 12-29-2004 09:43 AM
perl input field separator Tinkster Programming 5 10-18-2004 04:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration