www.webdeveloper.com
Results 1 to 3 of 3

Thread: function runs at browser reload.

  1. #1
    Join Date
    Jul 2012
    Posts
    12

    function runs at browser reload.

    I'm tinkering with css and javascript today but I've encountered a small problem.

    Here's a snippet of my code:

    PHP Code:
    var genres = new Array();
    var 
    genre1 = new Array();
    var 
    genre2 = new Array();
    var 
    genre3 = new Array();
        

    window.onload = function(){
        var 
    addButton document.getElementById("addButton");
        
    addButton.onclick handleAddButton;
        
        var 
    genre1 document.getElementById("genre1");
        
    genre1.onclick showGenreValues("genre1");
        
    }

    function 
    handleAddButton(){
        ...
    }

    function 
    pushValues(songInfoString,songGenre){
        ...
    }

    function 
    showGenreValues(genre){
        
    alert("Showing: " genre);

    I left out the content of functions because they are not part of the problem.

    the problem resides in

    PHP Code:
    function showGenreValues(genre){
        
    alert("Showing: " genre);

    this function should only be ran when pushing a subitem in a menu. Here's the snippet of the menu:

    Code:
    <nav>
     <ul>
       <li><a href="">Link1</a>
          <ul id="link1">
    	 <li id="genre1"><a href="">Genre1</a></li>
    	 <li id="genre2"><a href="">Genre2</a></li>
      	 <li id="genre3"><a href="">Genre3</a></li>
          </ul>
       </li>
       <li id="link2"><a href="">Link2</a></li>
       <li id="link3"><a href="">Link3</a></li>
       <li id="link4"><a href="">Link4</a></li>
       <li id="link5"><a href="">Link5</a></li>
     </ul>
    </nav>
    Now the showGenreValues function runs like it should when I press it in a browser. Problem is, when I hit reload I get the alert sent by the function as well, which I shouldn't.

    Anyone who knows how this is possible?

    Cheers.

  2. #2
    Join Date
    Jul 2007
    Posts
    386
    That is happening because you are passing an argument to the function.

    Try it like this:
    Code:
    var genre1 = document.getElementById("genre1");
    genre1.onclick = function(){showGenreValues("genre1")};

  3. #3
    Join Date
    Jul 2012
    Posts
    12
    Quote Originally Posted by SparoHawk View Post
    That is happening because you are passing an argument to the function.

    Try it like this:
    Code:
    var genre1 = document.getElementById("genre1");
    genre1.onclick = function(){showGenreValues("genre1")};

    Worked like a charm, thank you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles