views:

30

answers:

3

I am using CakePHP as my framework.

On page load, I want to force the cursor to a specific form field where name="data[Project][title]"

I'm trying to use javascript:

This works fine if I change the name to something without brackets, but fails to work with this form name. I have to use this form field name because of how CakePHP processes my form data.

Is there a workaround or another simple way to force the cursor to this form field?

Here is the code I currently have (if you change "data[Project][title]" to "formField" it works):

<body onLoad="document.searchForm.data[Project][title].focus();">

<form action="http://beta.industrialinterface.com/users/mainadd/" method="post" id="create-form" name="searchForm">

<input id="main-input" type="text" class="title-limit" name="data[Project][title]" onClick="this.value='';limitText(60);" onKeyDown="limitText(60);return true;" onKeyUp="limitText(60);return true;" />
+1  A: 

Example code? Because otherwise you could simply do:

document.getElementById('id_for_your_input').focus();
mhitza
@mhitza - It seems that he doesn't have/isn't capable of setting an `id` for the input (I'm gathering that CakePHP is generating the form for him).
clarkf
he can still set an id, though. cakePHP would allow that :)
mark
+1  A: 

If you are using a form and have access to the form element you can do

formelement['data[project][title]'].focus();

example at http://www.jsfiddle.net/fqgxv/

Gaby
A: 

You can use getElementsByName() for this. Note that it returns an array.

Ex:

<body onLoad="document.getElementsByName("data[Project][title]")[0].focus();">
clarkf