javascript function parametrs help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpmel
    New Member
    • Oct 2007
    • 69

    javascript function parametrs help

    Hi,

    i have a javascript function that changes the text in a label (ASP.NET form) to blank....It works fine just asit is shown below.
    However, I dont want to hard code the label name to the function. I want to be a ble to probably pass the label name as a parameter so that the function can work no matter what label it is. Can some one please help me add the modifications to it.

    Code:
    <script type="text/javascript">
            function changemsg(){
                document.getElementById('<%=AccountLabel.ClientID%>').innerHTML=" ";
            }
    </script>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    [code=javascript]
    function changemsg(id) {
    document.getEle mentById(id).in nerHTML=" ";
    }[/code]Pass the ID as a string when you call the function.

    Comment

    • phpmel
      New Member
      • Oct 2007
      • 69

      #3
      Thank you for your reply

      I tried it, however it does not work. I am working in ASP.NET so in my PageLoad method of my page i have to add the onFocus event of my Account textBox like this

      Account.Attribu tes.add("onFocu s","changemsg() ;")

      then the javascript for the changemsg method goes like this

      <script type="text/javascript">
      function changemsg(){
      document.getEle mentById('<%=Ac countLabel.Clie ntID%>').innerH TML=" ";
      }
      </script>

      It works just like how i stated above

      Not sure why it not working with your suggestion

      Any other suggestions ?

      Comment

      • phpmel
        New Member
        • Oct 2007
        • 69

        #4
        Hi guys,

        I figured it out, Here is the code

        This is the call to the function in the pageLoad method
        Account.Attribu tes.Add("onFocu s", "changemsg('ctl 00_ContentPlace Holder1_Account Label');");


        this is the javascript function
        <script type="text/javascript">
        function changemsg(lbl){
        document.getEle mentById(lbl).i nnerHTML=" ";

        }
        </script>

        Somehow, if you just pass the name of the Label as the parameter it was not being recognised because i used Master Pages. So I got the name from the source code when i ran the page and its name was "tl00_ContentPl aceHolder1_Acco untLabel"

        Anyway thanks alot for all your help!

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Originally posted by phpmel
          Somehow, if you just pass the name of the Label as the parameter it was not being recognised because i used Master Pages. So I got the name from the source code when i ran the page and its name was "tl00_ContentPl aceHolder1_Acco untLabel"
          Yes, it has to be the label id on the client-side. I hope by name you mean ID because if it's name you're using, it'll only work in IE, not in other browsers.

          Comment

          Working...