0

I'm working on a website which shows images. Eeach image has just one description which can be edited or added by any user. Accordingly, there are 2 forms: one for adding and the second for inserting at the same page. I displayed the form which I need and hid what I don't by javascript.

My question is how can I prevent the user from leaving the specific form (not all page and the same URL)?

To be clearer, I will tell you the senario precisely

IF (There is no description) 
showing adding button
 else (there is a description)
showing the description found ih the DB as a paragraph and editing button 

Once the user clicks the adding button, a form with a text area for adding will be shown. And the same goes with editing. I WANT TO PREVENT THE USER FROM LEAVING WHEN ONE OF THE FORMS IS SHOWN. Hopfully things are clearer now !

Thanks in Advance!!

MANAL
  • 1
  • 1

1 Answers1

0

I don't really get your question, but you could handle the blur() event to give focus back to the blurred control (focus()). If you want the user to be able to leave a control but within a single form you should check their parents (the one's who triggered the event and the one's who just gained the focus), if they don't have common parent get the focus back. This is working only if there are a single hierarchical level within the form (i.e: every control within the form are siblings), but there are many other ways to do the trick.

Note that you can not prevent the user from leaving the page. You can display however a warning. See this question.

Community
  • 1
  • 1
Máthé Endre-Botond
  • 4,826
  • 2
  • 29
  • 48
  • thank you for corporation! Yes, I know it is not a complete prevention. It is a confirming massege shown to user and depending on the user choice determines the effect of writing flag on DB. – MANAL Apr 25 '11 at 15:43
  • to be cleare, I will tell you the senario precisely IF (There is no description) showing adding button else (there is a description) showing the description found ih the DB as a paragraph and editing button Once the user clicks adding button, a form with a text area for add will be show. And the same goes with editing. I WANT TO PREVENT THE USER FROM LEAVING WHEN ONE OF THE FORMS IS SHOWN. Hopfully things are clear now ! thanks a million!! – MANAL Apr 25 '11 at 15:55
  • That kind of approach is a bad design for a website, users are usually dump, they click randomly, you should make the page resistible to stupidness, you need to allow the user to go on even if he clicked the add/edit button accidently, but yes, you can always show a warning that the information he had written will be lost if he doesn't submit the form (see link in the post). What's more don't expect anything from the user side. In short you can not prevent the user to completely leave the page, no browser will allow you that. Imagine if some/all websites did that. – Máthé Endre-Botond Apr 25 '11 at 17:53