SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onFocus="this.select()"

    hi
    i'm very newbie on javascript, but i came out with a script that puts the cursor at the upper left corner in a textarea.
    Code:
    <body onLoad="document.forms[0].elements['query'].focus()">
    <textarea onFocus="this.select()" cols="60" rows="5" name="query">
    But now i've a problem, since my web/php page has 2 forms.
    i tried
    Code:
    <body onLoad="document.forms[1].elements['a'].focus()">
    <textarea onFocus="this.select()" cols="60" rows="5" name="a">
    ,
    but it gives an error, asking to debug...
    How can i turn it around?

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    turn what around? what is the error you are getting?

    the first example shows an onload for the 1st form
    the second example show an onload for the 2nd form

    do you want to do both with 1 onload?

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've the site online (without this last javascript)
    The error message says:
    "An error hapenned during executiont.
    Do you want to debug?
    Line 64 (<body onLoad="document.forms[1].elements['otexto'].focus()"> )
    'Error:document.forms.1.elements.otexto' its null or its not an object."
    I think this is because i've an if else statement with php, and when the page first loads my textarea doesnt show. It does only after user inputs her name into a textbox and submits it. Only then the textarea shows up...
    here's the whole code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Livro de visitas</title>
    <meta http-equiv="Content-type"
    content="text/html; charset=ISO-8859-1" />
    <link rel = "Stylesheet" type="text/css" href = "/phpb.css" />
    <script type = "text/javascript">
    //<![CDATA[
    <!-- 
    function trim(value) {
    var temp = value;
    var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
    var obj = / +/g;
    temp = temp.replace(obj, " " );
    if (temp == " " ) { temp = ""; }
    return temp;
    }
    function VerificaLogIn(form) {
    if (trim(form.nome.value) == "" )
    {
    alert("Escreva o seu nome!" );
    return false;
    }
    return true;
    }
    //-->
    //]]>
    </script>
    </head>
    <body onLoad="document.forms[1].elements['otexto'].focus()">
    <div class = "central2">
    
    <?php 
    include('b.php');
    ?>
    <?php if ((!isset ($_GET['nome'])) or (($_GET['nome'])=="" )): ?>
    <form action = "<?=$_SERVER['PHP_SELF']?>" method = "get" onsubmit = "return VerificaLogIn(this);">
    <p class = "centro">Introduza o seu nome: <input type = "text" name = "nome" />
    <input type = "submit" value = "Entrar" /></p>
    </form>
    
    <?php else: ?>
    
    <div class = "centro">
    <form action = "ler.php?nome=<?=urlencode(stripslashes($_GET['nome']))?>" method = "post">
    <p>Escreva o seu comentário:<br /><br />
    <textarea onFocus="this.select()" name = "otexto" rows = "10" cols = "40"> 
    </textarea>
    <br />
    <br />
    <input type = "submit" name = "comentario" value = "Enviar" />
    <input type = "reset" value = "Limpar" />
    <input type = "hidden" name = "unico" value = "<?=md5(date('Ymdhis'))?>" />
    </p>
    </form>
    </div>
    
    <?php endif; ?>
    
    </div>
    </body>
    </html>
    EDITED
    here's the include code:
    Code:
    <?php include("on.php"); ?>
    <p class = "nav">
      <a class = "nav" href = "">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
      <a class = "nav" href = "vg.php">Gest&atilde;o</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
      <a class = "nav" href = "vs.php">Secretaria</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
      <a class = "nav" href = "vcr.php">Aconteceu</a>&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;
      <a class = "nav" href = "livro.php">Livro de visitas</a>
    </p>
      <div class = "floatvg">
      <div class = "busca">
       <form method = "get" action = "http://search.atomz.com/search/">
    	  <p>procurar neste site: <input size = "15" name = "sp-q" />
    	<input type = "submit" value = "&nbsp;&nbsp;ir&nbsp;&nbsp;" />
    	<input type = "hidden" name = "sp-a" value = "sp10024c49" />
    	<input type = "hidden" name = "sp-f" value = "ISO-8859-1" />
       </p>
    	</form> 
      </div> 
       <br />
       <br />
     
      <a class = "d" href = "">home</a><!-- &nbsp;
      <img src = "imagens/s.gif" alt = "" width = "5" height = "5" />&nbsp;índice -->
      </div>
      <div class = "logo"><img class = "border1" width = "183" height = "78" src = "imagens/aescola.jpg" alt = "O edifício" />    
    	<?php 
    	  if (!trim($_GET['nome'])=="") { 
    	   echo('Olá <strong>'.stripslashes(htmlspecialchars(strip_tags($_GET['nome']))).'</strong>!');
    	}
    	?>
      </div> 
      <div class = "dta"> 
    	ebi 1, 2, 3 /JI Vasco da Gama
      </div>

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    textbox and submits it. Only then the textarea shows up...
    I only glanced at the code since I'm not a phper.

    I can't say for certain, but it seems the first form (donde pregunta para el 'nome') doesn't exist for the onload event and the browser thinks the comentario form is forms[0].

    Give the forms names and access them with:
    document.forms['nome']
    document.forms['comentario']

    espero que te ayudad y pardone mes espanol
    Vicente
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    lisboa
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if i give forms a name, then the code doesnt validate...

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    which browser are you using? not IE, right?
    code doesnt validate...
    why? you are simply sending the form (this), right? A name attribute shouldn't matter.
    what about naming the second form? It isn't validating, right?

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if he is using XHTML 1.1, forms don't have a name attribute, so you should give the form an ID, and you should use document.getElementById('formId'), instead of document.forms['name'].
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •