0

DEMO

I have written a short script that takes the pagex and pageY coordinates of a mouse click and appends them to a list. Everything functions except the button that is supposed to clear the list( a click appearing even when the clear button is pressed also happens but that is not an issue)

anyway I corrected some work in jsfiddle and everything works fine there, but when I made the the same corrections in notepad++ the button doesnt function at all. IE it does not clear the list, but when I click the same button in jsfiddle it works fine. Have tested on mozilla Chrome and IE

         <!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title> Click Away</title>
    <link href="stylesheets/site.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>


        $(document).click(function(e){
            $("#coordList").append("<li>"+ "pageX: " + e.pageX + ",    pageY: " + e.pageY +"</li>"); 
        });

        $("#clear").click(function() {
            $("#coordList").empty();
        });
    </script>
</head>
<body>
    <div id="wrapper">
        <header>
            <h1> Coordinates </h1>
        </header>
        <button id="clear"> Clear list </button>
        <div id="list"> 
            <ol id="coordList"></ol>
              </div>
            </div>
              </body>
                  </html>  
nope
  • 177
  • 1
  • 4
  • 16

1 Answers1

1

I just learnt this five minutes ago. event.stopPropagation(); stops the event from triggering that of it's parents, including the body element. Like this:

$("#clear").click(function (event) {
  event.stopPropagation();
  $("#coordList").empty();
});

Demo

ambrosechua
  • 139
  • 1
  • 7
  • This works great. For some reason the code still does not work outside of JSfiddle so my problem lies somewhere else. Perhaps an invisible character? I'll just do a clean rewrite and see what happens – nope Oct 19 '13 at 01:18
  • just rewrote it and it still doesnt function outside of jssfiddle. Hmmm. I tried it with FF and chrome so far – nope Oct 19 '13 at 01:47
  • A possible problem: http://stackoverflow.com/questions/12719859/syntaxerror-unexpected-token-illegal – ambrosechua Oct 19 '13 at 01:48
  • Does it work in chrome and Mozilla? Any errors in console in IE? – ambrosechua Oct 19 '13 at 01:50
  • No errors in any browser, everything works except for the button. this is for an assignment so I'll just link the jsfiddle to my professor instead. I'm sure he'll accept it – nope Oct 19 '13 at 01:55