Astahost.com   Mar 20, 2010
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > JavaScript

Focus() And Select() Problems With Firefox

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > JavaScript

Focus() And Select() Problems With Firefox

vdesignlabs
Hi guys,

I am new to javascript and am currently learning it with the book Wrox Beginning Javascript 3rd ed.

I have just encountered a problem that I am not able to figure out. Could you please help me out.....

The problem is that select() and focus() are not working in FF2.0 but works in IE7.

This is what the code is supposed to do:
1. I enter a age
2. If I enter non numerical data and then I tab out of the text area
3. An alert box pops up and tells me to correct the age.
4. On hitting ok it goes back to the age field.

But in FF its going to the next tag(the submit button).

This is the code:

CODE
<script type="text/javascript">

function txt_age_onblur()
{
var in_txt_age = document.form1.txt_age;

if (isNaN(in_txt_age.value))
{
alert("Please enter a valid age");
in_txt_age.focus(); //looks as if no need for focus() as I am able to edit the text with only select() in IE. Not working with FF.
in_txt_age.select(); //Not working with FF
}
}
</script>
</head>
<body>
<form name=form1>
Age:
<input type=text name=txt_age onblur="txt_age_onblur()" size=3 maxlength=3 />
<input type=button value=Submit />
</form>
</body>
</html>


Thanks for you time friends. Smile..............

 

 

 


Comment/Reply (w/o sign-up)

vdesignlabs
Solved it!!!!!!!!!

I used this instead:
CODE
setTimeout("document.form1.txt_age.select()", 1);

and I can't understand why
CODE
document.form1.txt_age.select();

did not work!!!!!!!!!

If somebody can tell me why, it will be appreciated.

Thank You...............

Comment/Reply (w/o sign-up)

TavoxPeru
A time ago i have the same problem and i found that this happens because of the DOM and if you set or not the DOCTYPE to tell the browser which kind of page you serve. So, what i did to resolve this is to attach to the onsubmit event of my form the this reserved word, that references the form itself and of course declare correctly which page i will serve using the DOCTYPE.

This is the code that i use on my page:

CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $languageCode; ?>">
<head>
&lt;script  type="text/javascript">
//<![CDATA[
function validate_form1(theForm) {
    Tot = theForm.elements.length-3; // because i don't need to validate the last 3 fields of my form (2 buttons and one textfield)
    for(i = 0; i < Tot;i++) {
        if(theForm.elements[i].value == "") {
            alert("Error...blah blah blah");
            theForm.elements[i].focus();
            theForm.elements[i].select();
            return false;
        }
    }
    email=theForm.email_field_name.value;
    if (!isEmail(email)) {
        alert("Error email not valid!!!");
        theForm.email_field_name.focus();
        theForm.email_field_name.select();
        return false;
    }

    return true;
}
// other functions
//]]>
</script>
</head>
<body>
<!-- some content -->
<form action="sendmail.php" method="post" id="form1" onsubmit="return validate_form1(this);">
<!-- text inputs and the submit and reset buttons -->
</form>
<!-- some content -->
</body>
</html>

As you can see, this page is a valid XHTML webpage, please visit the following webpage Contact Us - Calendar Te Recuerdo Perú 2008 - Almanac Te Recuerdo Peru 2008 if you want to see it in action.

Of course, you can use this to reference not only your forms also you can use it with your fields, for example, you can attach to the onfocus event of your fields the select() function to select its contents directly like in the following code:

CODE
<input type="text" name="txt_age" onblur="txt_age_onblur()" size="3 maxlength="3" onfocus="this.select();" />

Best regards,

 

 

 


Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : focus, select, problems, firefox

  1. Settimeout() & Focus() Not Working With Firefox
    (1)
  2. Problems Dynamically Adding A Table With Dom
    (4)
    I'm writing a search function to search a MySQL database I have. The search uses AJAX with a
    php server to search the database and generate a result. The result is passed back as a DOM object
    to the javascript, which then parses the result into an xhtml table. I then add the table to my
    page. This works just fine under Firefox. However, under IE (version 7, so I assume others as
    well), the table doesn't display. I used the IE Developer Toolbar to look at the DOM, and the
    table has been successfully added to the DOM, but it isn't showing up. Interesting....
  3. Problems With Dynamically Loading Javascript
    As well as dynamically inserting HTML (2)
    Hello, I have started to try to create a JavaScript application (or rather, AJAX, but the
    JavaScript is the part I'm having trouble with). I have come across a roadblock, though. I try
    to load an external JavaScript file by editing the innerHTML of a div tag to contain <script
    src="URL" type="text/javascript"> (with a real URL). The problem is that it is not loading. I have
    used FireBug to check the dynamic HTML status, I get the following (with code removed): CODE
    <script type="text/javascript"> <script src="source/main_login.js" typ....
  4. Formatting Alerts/confirm In Firefox 1.5.0.1
    (3)
    I was trying to use the \t to tab in Firefox 1.5.0.1. It works in IE 6. I haven't tried it in
    other other browser, but I was wondering if there's something I'm doing wrong. I tried a
    simple test page to see if it was my script, and the test page gave me the same results. It worked
    in IE6 but not Firefox. I also tried putting in " " for tabs instead of using the \t escape.
    Firefox just ignores the white space I guess. Any ideas on formatting it? I'll post the test I
    did: CODE function test(){ alert("\t\tCENTER!\t\t"); } --> ....
  5. Alpha Release Astahost Credits Reporting Extension
    for Firefox, of course (9)
    Hi guys, I'm almost finished with coding an extension for Firefox that'll enable Astahost
    Hosted Members (and later on Trap17 too) to view their current hosting credits count right off the
    status bar of their favourite browser - even if you're NOT visiting astahost. This will
    always keep you upto date with your credits count - and alert you whenever your credits count dips
    below the safe zone. The extension comes with 4 different coloured icons - BLUE, GREEN, AMBER and
    RED - which gets displayed along with your credits. It is meant to provide a visual in....
  6. Need Drop Down Menu Script
    Current not working in firefox (1)
    I'm in need of a drop down menu script. The one I am currently using was written to work in
    both ie and netscape, but does not work in firefox. /sad.gif" style="vertical-align:middle"
    emoid=":(" border="0" alt="sad.gif" /> I new at installation that the author had stretched the
    rules abit to make it work, but didn't expect firefox to come out. I don't care what
    language it is written in as long it works If you have ie you can see an example at:
    http://66.78.26.33/~ruidoso/ I would appreciate any help you can recommend pete....

    1. Looking for focus, select, problems, firefox



See Also,

*SIMILAR VIDEOS*
Searching Video's for focus, select, problems, firefox
advertisement




Focus() And Select() Problems With Firefox

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com



Creative Commons License