开发者

How do I use Javascript to force cursor to specific form field when form field has a name with brackets?

开发者 https://www.devze.com 2023-01-21 01:35 出处:网络
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 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 th开发者_JAVA技巧is 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;" />


Example code? Because otherwise you could simply do:

document.getElementById('id_for_your_input').focus();


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/


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

Ex:

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

精彩评论

暂无评论...
验证码 换一张
取 消