Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. getElementById() when element is in MasterPage

getElementById() when element is in MasterPage

Scheduled Pinned Locked Moved Web Development
javascriptquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mundo Cani
    wrote on last edited by
    #1

    Hello, I have a 'select' element in the footer of my masterpage. I'm trying to write javascript to assign a handler to the 'onchange' event of the 'select' control. The javascript is as follows: var selectDisplay = document.getElementById('selectDisplay'); selectDisplay.onchange = function() { alert("You did it!"); } The id of the 'select' element is 'selectDisplay'. I have found references to issues regarding using getElementById when the element you want is in the masterpage, but have been unable to find a solution. Any ideas? Thanks!

    Ian

    J G L 3 Replies Last reply
    0
    • M Mundo Cani

      Hello, I have a 'select' element in the footer of my masterpage. I'm trying to write javascript to assign a handler to the 'onchange' event of the 'select' control. The javascript is as follows: var selectDisplay = document.getElementById('selectDisplay'); selectDisplay.onchange = function() { alert("You did it!"); } The id of the 'select' element is 'selectDisplay'. I have found references to issues regarding using getElementById when the element you want is in the masterpage, but have been unable to find a solution. Any ideas? Thanks!

      Ian

      J Offline
      J Offline
      JimmyRopes
      wrote on last edited by
      #2

      Mundo Cani wrote:

      selectDisplay.onchange

      :confused: Try setting it up something like this: Untitled   function OptionSelect(){   var sel = document.getElementById("selectDisplay");     for(i = 0; i < sel.length; i++){       if(sel[i].selected){         i ? alert('Option ' + sel.value + ' selected') : alert('Select an Option');         break;       } // if(sel[i].selected)     } // for(i = 0; i < sel.length; i++)   } // OptionSelect()    onchange="OptionSelect();">   Select an Option    Option 1   Option 2   Option 3   Option 4

      Simply Elegant Designs JimmyRopes Designs
      Think inside the box! ProActive Secure Systems
      I'm on-line therefore I am. JimmyRopes

      M 1 Reply Last reply
      0
      • J JimmyRopes

        Mundo Cani wrote:

        selectDisplay.onchange

        :confused: Try setting it up something like this: Untitled   function OptionSelect(){   var sel = document.getElementById("selectDisplay");     for(i = 0; i < sel.length; i++){       if(sel[i].selected){         i ? alert('Option ' + sel.value + ' selected') : alert('Select an Option');         break;       } // if(sel[i].selected)     } // for(i = 0; i < sel.length; i++)   } // OptionSelect()    onchange="OptionSelect();">   Select an Option    Option 1   Option 2   Option 3   Option 4

        Simply Elegant Designs JimmyRopes Designs
        Think inside the box! ProActive Secure Systems
        I'm on-line therefore I am. JimmyRopes

        M Offline
        M Offline
        Mundo Cani
        wrote on last edited by
        #3

        JimmyRopes wrote:

        var sel = document.getElementById("selectDisplay");

        Thanks for your reply. Unfortunately, the problem is getElementById does not return an element. I pass it the id of the element I'm looking for (there is no mispelling... I copy/pasted the id) but it is not returned. :doh:

        Ian

        J G 2 Replies Last reply
        0
        • M Mundo Cani

          Hello, I have a 'select' element in the footer of my masterpage. I'm trying to write javascript to assign a handler to the 'onchange' event of the 'select' control. The javascript is as follows: var selectDisplay = document.getElementById('selectDisplay'); selectDisplay.onchange = function() { alert("You did it!"); } The id of the 'select' element is 'selectDisplay'. I have found references to issues regarding using getElementById when the element you want is in the masterpage, but have been unable to find a solution. Any ideas? Thanks!

          Ian

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          When a control is in a container (like a content control of a master page), the id is prepended with the id of the container. Use the ClientID property of the control to get the id that you can use in client script, or view the source of the generated page to find out the generated id.

          --- single minded; short sighted; long gone;

          1 Reply Last reply
          0
          • M Mundo Cani

            Hello, I have a 'select' element in the footer of my masterpage. I'm trying to write javascript to assign a handler to the 'onchange' event of the 'select' control. The javascript is as follows: var selectDisplay = document.getElementById('selectDisplay'); selectDisplay.onchange = function() { alert("You did it!"); } The id of the 'select' element is 'selectDisplay'. I have found references to issues regarding using getElementById when the element you want is in the masterpage, but have been unable to find a solution. Any ideas? Thanks!

            Ian

            L Offline
            L Offline
            Lalafaraz
            wrote on last edited by
            #5

            Ian When control is in master page then the client side rendering of the webpage automatically genrate Id's for the server side control you need to traverse all the elements in the content regardless of using "document.getElementById('selectDisplay');" coz it will definetly not work Thanks Sarfaraz Nazir Solution Developer Accentys Inc

            M 1 Reply Last reply
            0
            • M Mundo Cani

              JimmyRopes wrote:

              var sel = document.getElementById("selectDisplay");

              Thanks for your reply. Unfortunately, the problem is getElementById does not return an element. I pass it the id of the element I'm looking for (there is no mispelling... I copy/pasted the id) but it is not returned. :doh:

              Ian

              J Offline
              J Offline
              JimmyRopes
              wrote on last edited by
              #6

              Mundo Cani wrote:

              Unfortunately, the problem is getElementById does not return an element.

              That is very strange. :confused: As an exercise I just swiped the code in my rely from the <html> to the </html> and put it into a new document in notepad. I saved the document as test.htm and then clicked on the saved document. A browser window came up and the control worked as intended. I didn't add or take anything away but just used the code posted. Try it and see if you get the same results. :-D There is no problem with getElementById. I use it all the time on my web pages.

              Simply Elegant Designs JimmyRopes Designs
              Think inside the box! ProActive Secure Systems
              I'm on-line therefore I am. JimmyRopes

              M 1 Reply Last reply
              0
              • M Mundo Cani

                JimmyRopes wrote:

                var sel = document.getElementById("selectDisplay");

                Thanks for your reply. Unfortunately, the problem is getElementById does not return an element. I pass it the id of the element I'm looking for (there is no mispelling... I copy/pasted the id) but it is not returned. :doh:

                Ian

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Mundo Cani wrote:

                there is no mispelling... I copy/pasted the id

                That doesn't help, as you are copying the id that is used for the server control. The getElementById method is using the id of the html element that the control renders, and if you examine the rendered html code (view source in the browser) you will see that they are not the same.

                --- single minded; short sighted; long gone;

                M 1 Reply Last reply
                0
                • G Guffa

                  Mundo Cani wrote:

                  there is no mispelling... I copy/pasted the id

                  That doesn't help, as you are copying the id that is used for the server control. The getElementById method is using the id of the html element that the control renders, and if you examine the rendered html code (view source in the browser) you will see that they are not the same.

                  --- single minded; short sighted; long gone;

                  M Offline
                  M Offline
                  Mundo Cani
                  wrote on last edited by
                  #8

                  Thanks!

                  Ian

                  1 Reply Last reply
                  0
                  • L Lalafaraz

                    Ian When control is in master page then the client side rendering of the webpage automatically genrate Id's for the server side control you need to traverse all the elements in the content regardless of using "document.getElementById('selectDisplay');" coz it will definetly not work Thanks Sarfaraz Nazir Solution Developer Accentys Inc

                    M Offline
                    M Offline
                    Mundo Cani
                    wrote on last edited by
                    #9

                    Thanks for your help!

                    Ian

                    1 Reply Last reply
                    0
                    • J JimmyRopes

                      Mundo Cani wrote:

                      Unfortunately, the problem is getElementById does not return an element.

                      That is very strange. :confused: As an exercise I just swiped the code in my rely from the <html> to the </html> and put it into a new document in notepad. I saved the document as test.htm and then clicked on the saved document. A browser window came up and the control worked as intended. I didn't add or take anything away but just used the code posted. Try it and see if you get the same results. :-D There is no problem with getElementById. I use it all the time on my web pages.

                      Simply Elegant Designs JimmyRopes Designs
                      Think inside the box! ProActive Secure Systems
                      I'm on-line therefore I am. JimmyRopes

                      M Offline
                      M Offline
                      Mundo Cani
                      wrote on last edited by
                      #10

                      I got it working. I was confused with the difference between client-side and server-side controls. Thanks!

                      Ian

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups