Call documentGetElementById without event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KeredDrahcir
    Contributor
    • Nov 2009
    • 426

    #1

    Call documentGetElementById without event

    I need to change the background of an input box depending on if a radio button on a form has been selected or not.
    I'm trying to do it using a documentGetElem entById which is called when the php checks the radio buttons. There is no event to call the documentGetElem entById. Can I still call it without an event?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    a) the call is document.getEle mentById() (JavaScript is case sensitive)
    b) if you do processing with PHP, it’s too late for JavaScript.

    Comment

    • KeredDrahcir
      Contributor
      • Nov 2009
      • 426

      #3
      Is there some way I can change the style of something using php similar to the way Javascript uses;
      documentGetElem entById('id').s tyle.background Color='#df8ff9' ;

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        no. PHP and Javascript have a totally different way of "interactin g" with HTML.

        Comment

        • KeredDrahcir
          Contributor
          • Nov 2009
          • 426

          #5
          All I want to do as the page load is to check a POST value and if it is unset or one value set the background colour of an input box, and if it is another value set the background colour of a different box.
          Surely there must be a simple way of doing this.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            if you want to check a POST value, you need a server-side language like PHP or ASP. then everything else is just a modification of the source code.

            Comment

            • KeredDrahcir
              Contributor
              • Nov 2009
              • 426

              #7
              Define the boxs you want to change will their ID name. After the boxes use something like this:
              Code:
              <?
                if ((!strcmp($_POST['preferred'],"email")) || (!($_POST['preferred'])))
                {
                  echo "<script type="text/javascript">
                    document.getElementById('email').style.backgroundColor='#d7ebff'; document.getElementById('phone').style.backgroundColor='#ffffff';
                  </script>";
                }
                else
                {
                  echo "<script type="text/javascript">
                    document.getElementById('phone').style.backgroundColor='#d7ebff'; document.getElementById('email').style.backgroundColor='#ffffff';
                  </script>";
                }
              ?>

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                I wouldn’t use JavaScript, if I could edit the CSS right away.
                Code:
                <?php
                $color1 = '#d7ebff';
                $color2 = '#fff';
                if ((!strcmp($_POST['preferred'],"email")) || (!($_POST['preferred'])))
                {
                $color1 = '#fff';
                $color2 = '#d7ebff';
                }
                ?>
                
                #email {
                    background-color: <?= $color1; ?>;
                }
                
                #phone {
                    background-color: <?= $color2; ?>;
                }

                Comment

                Working...