tags:

views:

51

answers:

2

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.

A: 

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

Josh
thanks, but this is not what I need for the issue..it's set now. please see the comment below
Elaine
+1  A: 

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
} 
rahul
thanks,i got from MSDN that there indeed exists "defaultFocus" and "defualtButton" for form tag.Anyway, thanks. your solution is an good alternative.
Elaine