SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Help with using getElementById()

    Hey all,
    I have this problem and when I came looking for help, I checked out the other thread like this but I couldn't get it to work. Here is the function -
    PHP Code:
    function newForm(focusfield)
    {
        if ( 
    focus == 'no' )
        {
            if ( 
    document.getElementById(field).innerText == '' )
            {
                
    document.getElementById(field).innerText '____________';
            }
        }

        if ( 
    focus == 'yes' )
        {
            if ( 
    document.getElementById(field).innerText == '____________' )
            {
                
    document.getElementById(field).innerText '';
            }
        }

    and here is the form -
    PHP Code:
    <b>Field 1:</b> <input type='text' name='field1' class='textbox' value='____________' onFocus="newForm('yes', 'field1')" onFocusOut="newForm('no', 'field1')" /><br /><br />
    <
    b>Field 2:</b> <input type='text' name='field2' class='textbox' value='____________' onFocus="document.form.field2.innerText=''" onFocusOut="document.form.field2.innerText='____________'" /><br /><br /> 
    field1 uses the function but field2 uses just onFocus and onFocusOut but as you can see, it won't work.

    Thanks for the help

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this instead
    PHP Code:
    <html>
      <
    head>
        <
    title></title>

        <
    script type="text/javascript">

          function 
    focusElements(whatval)
          {
            (
    val)? what.innerText '' what.innerText '____________';
            
    //alert(what.innerText);
          
    }

        
    </script>
      </head>
      <body>
        <b>passing refernce:</b>
        <input type='text' name='field1' class='textbox' value='____________' onFocus="focusElements(this, true);" onFocusOut="focusElements(this, false);" /><br /><br />
        <b>inline:</b>
        <input type='text' name='field2' class='textbox' value='____________' onFocus="this.innerText='';" onFocusOut="this.innerText='____________';" /><br /><br />
      </body>
    </html> 

  3. #3
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Thats for that

    I've modified it a bit -
    PHP Code:
    function focusElements(whatval)
    {
        if ( 
    what.innerText == '' || what.innerText == '____________' )
        {
            (
    val) ? what.innerText '' what.innerText '____________';
        }

    because I only want it to change back to _______ if the user hasn't entered any text. The if-then I have added seems to make no difference but I am a PHP coder trying to learn JavaScript so the syntax might be a little wrong somewhere.

    Thanks for the help

  4. #4
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u forgot to assign the value back to whatever u want..

    PHP Code:
    function focusElements(whatval)
    {
        if ( 
    what.innerText == '' || what.innerText == '____________' )
        {
            
    what.innerText = (val) ? what.innerText '' what.innerText '____________';
        }

    i wouldnt recommend .innerText just use .value since its pretty simple input type='text' but i think it works anyway so no harm.

    gl hf

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting. Is there any reason why you have accessed innerText instead of value ? My first instinct would be to set the value property for a form element.

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face yawn...

    I didn't pickup on that, too early in the morning...

    PHP Code:
    <html>
      <
    head>
        <
    title></title>

        <
    script type="text/javascript">

          function 
    focusElements(whatval)
          {
            if ( 
    what.value == '' || what.value == '____________' )
            {
              (
    val) ? what.value '' what.value '____________';
            }
          }

        
    </script>
      </head>
      <body>
        <b>passing refernce:</b>
        <input type="text" name="field1" class="textbox" value="____________" onFocus="focusElements(this, true);" onFocusOut="focusElements(this, false);" /><br /><br />
      </body>
    </html> 

  7. #7
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    I don't know whay I was usin innerText, I just saw that used somewhere. Thanks for the help but I have decided on something else.

    Thanks again


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
  •