Forum Moderators: open

Message Too Old, No Replies

onclick focus element - Works only with name not id attribute!

         

JAB Creations

6:52 am on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I want to do what this script does except how can I do it without the name attribute?

<form name="myform2">
<input type="text" name="mytextfield2">
<input type="button" name="mybutton" value="Set Focus" OnClick="document.myform2.mytextfield2.focus();">
</form>

- John

[edited by: JAB_Creations at 6:53 am (utc) on May 19, 2007]

rocknbil

7:40 am on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm presuming you only want to eliminate the name so it doesn't submit? Does this work? (yessir, it does. :-) )

<form>
<input type="text" id="mytextfield2">
<input type="button" id="mybutton" value="Set Focus" OnClick="document.getElementById('mytextfield2').focus();">
</form>

Or anonymously

<form>
<input type="text">
<input type="button" id="mybutton" value="Set Focus" OnClick="this.form.elements[0].focus();">
</form>

That one's a little sloppy and hard to maintain though. :-)

JAB Creations

8:17 am on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, that's good but now I'm at a loss. I was actually using this script as a base for clicking on one anchor to give focus to a form element elsewhere on the page. How would I do that?

- John

Dabrowski

1:56 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just use the onClick statement from the <input> on your link.

rocknbil

5:27 pm on May 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^^^ To elaborate, you can do this so that if Javascript is disabled, it will still move to the relevant portion of the page using an anchor.

<a name="totohere" id="gotohere" href="#andhereIam" OnClick="document.getElementById('mytextfield2').focus(); return false;">Go to my text field</a>

<a name="andhereIam" id="andhereIam"></a>
<input type="text" id="mytextfield2">

If Javascript is enabled, it will ignore href="#", if disabled, it will go the the portion of that page that contains the #.

You're really even better off defining a function in an external javascript for these objects so the JS is unobtrusive - see the discussion about attachClickEvents() in this thread [webmasterworld.com].

JAB Creations

2:28 am on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, this works great!
<a href="#" onlick="document.getElementById('givemefocusplease').focus();">give another element focus</a>

<input id="givemefocusplease" type="checkbox" />

Thanks!

- John

rocknbil

5:05 pm on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK now **A** "right" way. Add a new function for every focus object in setFoci. Works with or without Javascript.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Give Me Focus</title>
<script type="text/javascript">
function setFoci() {
if (document.getElementById) {
document.getElementById('focus_link').onclick = function() { document.getElementById('givemefocusplease').focus(); }
}
}
window.onload = function() { setFoci(); }
</script>
</head>
<body>
<a href="#focus_anchor" id="focus_link">give another element focus</a>
<br>
<p>add more breaks here to test</p>

<a id="focus_anchor"></a>
<input id="givemefocusplease" type="checkbox">
<br>
<p>add more breaks here to test</p>
</body>
</html>

Dabrowski

7:26 pm on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nicely done rocknbil.

Score 1 for unobtrusive JS!

JAB Creations

7:29 pm on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't have time to test it at this moment but there is a downside to unobtrusive JavaScript as much as I like it: bloat. I've decided for unique or almost unique instances on my current work to leave the script on the page itself if I will not be switching the engine of the function. However when I switch the engine of the function (DHTML engine for visual effects) I will always use unobtrusive JavaScript. It's a double-edged sword. I'll try your code later tonight after work. Thanks for the replies!

- John

rocknbil

12:45 am on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, that JS is meant to be in an external file - and although I guess the javascript itself grows a little, the opposite is true of the document itself. It can finally fit back into that bikini. :-P

JAB Creations

12:49 am on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it's in the head of the document then yes, easy enough to know that. If I had lots of specific click/focus points I would see the need to somehow create an array to keep the modularized version down but I'm not at that point nor am I completely finished with the latest version of the GUI I'm working on. It is very interesting to see how JavaScript can be modularized though and I'm sure I'll be doing more of it in the future.

- John

Dabrowski

11:00 am on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you'd rather write quick JS than good JS?

JAB Creations

7:41 pm on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LoL no, in regards to what I'm working on I'm still building the base of the site and adding the site functions, etc. That means all the JavaScript is sort of like the ocean tides, some comes in and some goes out. Once things settle I'll be able to figure out how to optimize the best balance. I am thinking at the moment that I could eventually assign functions for the most common functions I need. I'm all for quality but I'm still working on it at this point. ;)

- John

Dabrowski

9:35 pm on May 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that I could eventually assign functions for the most common functions I need

Not really sure what this means?

JAB Creations

4:23 am on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



onclick="function1();" onkeypress="function1();"

Like we have discussed previously, the code I am using for the things I am doing would not generate any errors both in function nor any in the JavaScript console.

- John

Dabrowski

10:49 am on May 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Aah, you mean assign functions to events. ok I see. And no, if your code produced any errors it would be broken!