Solved

HTML definition List (DL) onclick event problem

Posted on 2009-04-08
4
956 Views
Last Modified: 2012-06-22
I am learning HTML and Javascript. I encountered an issue when I ran the attached code. If I select the bottom drop down list item, the debug message for the definition list (dl) event handler (markLoadPage) is popped up instead of the one (setColor) for the drop down list. This issue won't show if I move the drop down list to the top of the definition list. Is there anything wrong for the dl onclick event syntax? Thanks!
<html>
  <head>
    <title>Test</title>
    <script language="javascript"> <!--
 
      function markLoadPage( page_url )
      {
	    alert( 'markLoadPage() called!' );
      }
      
      function setColor()
     {
	  alert( 'setColor() called!' );
	  	  
	    var idx = document.getElementById( "color_list" ).selectedIndex;
	    if( idx > 0 )
	    {
	      document.bgColor = document.getElementById( "color_list" ).options[ idx ].value;
          }
      } 
//--></script>
  </head>
  <body>
   <form action="">
     <h2>Categories</h2>
     <dl><a href="red_rose.html" target="P3" onclick="markLoadPage( 'red_rose.html' )">Red rose</dl>
     <dl><a href="pink_rose.html" target="P3" onclick="markLoadPage( 'pink_rose.html' )">Pink rose</dl>
     <dl><a href="yellow_rose.html" target="P3" onclick="markLoadPage( 'yellow_rose.html' )">Yellow rose</dl>
     <dl><a href="white_rose.html" target="P3" onclick="markLoadPage( 'white_rose.html' )">White rose</dl>
     <dl><a href="orange_rose.html" target="P3" onclick="markLoadPage( 'orange_rose.html' )">Orange rose</dl>
     <dl><a href="lavender_rose.html" target="P3" onclick="markLoadPage( 'lavender_rose.html' )">Lavender rose</dl>
     <p/><p/>
     <select name="color_list" id="color_list" onchange="setColor()">
       <option value="prompt">Choose a colour</option>
       <option value="red">red</option>
       <option value="pink">pink</option>
       <option value="yellow">yellow</option>
       <option value="white">white</option>
       <option value="orange">orange</option>
       <option value="lavender">lavender</option>
     </select>
     <p/>
     <input type="button" class="bt_style" value="Save Selection" name="bt_save">  
   </form>
  </body>
</html>

Open in new window

0
Comment
Question by:gs_kanata
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
GarthSnyder earned 200 total points
ID: 24104444
Your <a> tags are unclosed, so the browser sees them as extending all the way to the end of the document. So the entire <select> popup is included in the link for lavender_rose and therefore has an onclick event.

Just close the <a>'s with </a> and you should be fine.
0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 200 total points
ID: 24104572
The HTML validator can be quite helpful: http://validator.w3.org/
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 100 total points
ID: 24105184
I also strongly advice to change


      function markLoadPage( page_url )
      {
            alert( 'markLoadPage() called!' );
            return false; // cancel the link
      }

and

 onclick="return markLoadPage( '...' )"
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24108133
Why "B" ???
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access RV042 GUI / Browser Issues 25 48
SSRS 2016 Rendering HTML tables 3 29
JavaScript Sort Ordering and Choice Limiting 5 23
Visual Studio npm 1 12
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question