Web Analytics Made Easy -
StatCounter newbie: setting onClick property - CodingForum

Announcement

Collapse
No announcement yet.

newbie: setting onClick property

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie: setting onClick property

    This is surely a simple RTFM problem, but I still can't do it
    Code:
    <HTML>
    <BODY onLoad="loader()">
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Hide code from older browsers
    function loader() {
      toc = document.getElementById("tableOfContents")
    
      for (i = 0; i < toc.all.length; i++) {
         if (toc.all[i].tagName == "H1") {toc.all[i].onclick="head()"}
      }
    }
    function head() {
       alert('hello mum')
    }
    // End hiding-->
    </SCRIPT>
    <!--
    </form>-->
    <div id="tableOfContents">
    <h1> here's a heading 1</h1>
    <h2 onclick="head()">here's a heading2</h2>
    </div>
    </BODY>
    </HTML>
    The 'heading 2' click works fine, but I want to be able to set them all (to keep the body of the markup as simple as possible) but my code has no effect. No doubt it's a simple error...

  • #2
    if (toc.all[i].tagName == "H1") {toc.all[i].onclick=head}
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

    Comment


    • #3
      Originally posted by Kor
      if (toc.all[i].tagName == "H1") {toc.all[i].onclick=head}
      Thank you.

      I had tried toc.all[i].onclick=head() but that of course invokes the function; at the risk of showing myself terminally stupid, how would one code the function to be invoked with an argument list?

      Comment


      • #4
        at the risk of showing myself terminally stupid
        np, sometimes after a long tiresome day, we all make stupid things. I remember me staying stucked about 2 hours trying to see what's wrong with

        i<array.lenght
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          Originally posted by Roy Gardiner
          how would one code the function to be invoked with an argument list?
          You mean like this?

          obj.onclick=function(){someFunc(arg1, arg2)};
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            Originally posted by glenngv
            You mean like this?

            obj.onclick=function(){someFunc(arg1, arg2)};
            Exactly, thank you.

            Newbie indeed; I couldn't even RTFM to find it and I've more FMs than the British Library.

            This is a fine forum, thank you ladies and gentlemen, each time I've asked a question here I've had good answers

            Comment

            Working...
            X