Elaine
Elaine

Reputation: 1318

how to set default focus?

Now one Submit button is the default focused control. but I need to set the default focus to another imagebutton.

Here is the sample image "button"

<img style="vertical-align: top; border-style: none;"                                                   src="IconAdd.jpg" id="imbAdd" onclick="javascript:Add();" title="Click to Add" style="cursor:hand;" />

But i use

$(document).ready(function(){$("#imbAdd").focus();}

It doesn't work.

I hope i'm clear. i need to implement when I press Enter, i can trigger "click" event for this image "button".

Thanks


Solution: I use <input type="image" id="imbAdd" onclick="addEntry();return false;"> .. instead of the image link. and set <form id="form1" defaultButton="imbAdd"> but this type button will cause submit which is not what I want. so i need to add "return false;" at the end "addEntry()" to make sure the image button will never cause any undesired submit.

Upvotes: 0

Views: 8997

Answers (3)

user669677
user669677

Reputation:

since HTML5:

<button type="button" autofocus>
Click Me!
</button>

Upvotes: 5

rahul
rahul

Reputation: 187110

You can't set focus to every element.

Its better you write a function that will be triggered on the keydown event and check for "Enter" key and the trigger the click event of the image.

in your keyup event check for this

var kCode = (e.keyCode ? e.keyCode : e.which); 

if (kCode == 13) 
{ 
   // its the enter key pressed
} 

Upvotes: 1

Josh
Josh

Reputation: 12722

There's something called tabindex that works on a variety of browsers. I use it to skip over images in a captcha for form focus. Its a first class attribute. It may give you the behavior you want if you reorder the tabindex/focus order of the two buttons.

<input type="text" name="test_field_1" tabindex=2></input>
<input type="text" name="test_field_1" tabindex=1></input>

Josh

Upvotes: 0

Related Questions