1

I am trying to show a floated div near a button when I click it, and hide the div when I hit a "Close" span within it. This is my code:

    <style>
    #noteDiv {display:none; position: absolute;}
    </style> 

    <script>
    function showNote(e) {

      var x = 0, y = 0;
      if (!e) e = window.event;
      if (e.pageX || e.pageY) {
        x = e.pageX;
        y = e.pageY;
      }
      else if (e.clientX || e.clientY) {
        x = e.clientX + document.body.scrollLeft
          + document.documentElement.scrollLeft;
        y = e.clientY + document.body.scrollTop
          + document.documentElement.scrollTop;
      }
    var noteDiv = document.getElementById("noteDiv");
      noteDiv.style.display = "block";
      noteDiv.style.left = (x+20)+"px";
      noteDiv.style.top = (y)+"px";
    }

    function hideNote() {
      document.getElementById("noteDiv").style.display = "none";
    }
    </script>

 <div id='noteDiv'>
 <div style="margin: 5px; float: right; font-size: smaller"><span  onclick="hideNote()">Close</span></div><br clear="all">

<div>Note Content</div>
 </div>

    <?php
    echo "<button type ='button' onClick = 'showNote();'>Note</button>";
    ?>

It does hide the div, but when I click the button "Note", the noteDiv does not show up.

What is going wrong?

Thank you! Michael Laszlo. I Think I found the problem though I don't know how to deal with it.

I believe it is the javascript peice since if I comment out this part:

if (e.pageX || e.pageY) {
  x = e.pageX;
  y = e.pageY;
 }
  else if (e.clientX || e.clientY) {
  x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  y = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
  }

The button works well (although the position is at the left corner of the screen since the x and y are 0).

Did I miss anything?

For Sunil's suggestion,I removed the class part for now, and I changed the onClick to onclick, and the echo is good now, Thank you Sunil!

Thank you Michael Laszlo, again! Yes you are right! I need to give the button an ID or the e could not be passed to the function! Now my code works, too!

I have a lot of such buttons on my page, and I need to generate a var to verify them and then I think it should work. If any problems, it should be another question, haha, about php.

Thank you so much Michael Laszlo and Sunil, your suggestions of setting ids to the button and checking the code from browser are great help! I learned valuable things from you guys!

  • Did you inspect the html in the browser's developer tool? That will tell you whether the Note button is rendering with proper html. Use your original code to get this. Then its easy to know what's wrong. – Sunil Aug 17 '15 at 20:36
  • your problem is the embeded $classStr, which is causing the browser to not recognize the onclick attribute and therefore, not firing the code for onclick. – Sunil Aug 17 '15 at 20:56
  • On reason for incorrect html being rendered with $classDir in your case could be that the apache web server is not configured for php properly. So this is actually a php problem and not a JavaScript problem. – Sunil Aug 17 '15 at 21:01
  • Can you paste the code for the class of req_COMP? This should be some CSS defined somewhere. There could be some issue with your class CSS. – Sunil Aug 17 '15 at 21:08
  • Also, look at the script that renders. May be the JavaaScript is not rendering properly in your page and as a result the showNote function is not recognized. – Sunil Aug 17 '15 at 21:25

2 Answers2

1

The problem is that the event e is undefined when showNote() gets called. To ensure that an event is passed to showNote, give the button an ID and attach the click handler when the window loads.

For example, you can define the button like this:

<button id="noteButton" type="button">Note</button>

Then add this to your JavaScript:

window.onload = function () {
  document.getElementById('noteButton').onclick = showNote;
};

This approach is demonstrated in the following snippet.

window.onload = function () {
  document.getElementById('noteButton').onclick = showNote;
};

function showNote(e) {
  var x = 0, y = 0;
  if (!e) e = window.event;
  if (e.pageX || e.pageY) { 
    x = e.pageX;
    y = e.pageY;
  } 
  else if (e.clientX || e.clientY) { 
    x = e.clientX + document.body.scrollLeft
      + document.documentElement.scrollLeft;
    y = e.clientY + document.body.scrollTop
      + document.documentElement.scrollTop;
  } 
  var noteDiv = document.getElementById("noteDiv");
  noteDiv.style.display = "block";
  noteDiv.style.left = (x+20)+"px";
  noteDiv.style.top = (y)+"px";
} 

function hideNote() { 
  document.getElementById("noteDiv").style.display = "none";
}
#noteDiv {
  display:none;
  position: absolute;
}
<div id='noteDiv'>
  <div style="margin: 5px; float: right; font-size: smaller">
    <span onclick="hideNote()">Close</span>
  </div>
  <br clear="all">
  <div>Note Content</div>
</div>

<button id="noteButton" type="button">Note</button>
Michael Laszlo
  • 12,009
  • 2
  • 29
  • 47
0

It should work, but keep in mind that you need to click a specific area on screen and not just anywhere to close the note. You could make this area obvious to user by styling the span with a border.

You can see a demo to show that it works here: https://jsfiddle.net/g43j5jdp/1/

I added a border to the close area as in code below.

<div style="margin: 5px; float: right; font-size: smaller;
border:1px solid red;"><span onclick="hideNote()">Close</span>

NOTE The quickest solution to your problem would be to use the browser's developer tool. Else you might end up spending too much time on this.

Also please read this stackoverflow post, which might help you to get rid of php code $classStr showing in rendered html: Why is my PHP source code showing? Your problem seems to be with php configuration.

Community
  • 1
  • 1
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • Thank you Sunil. It is a good suggestion! But the noteDiv still does not show. – user5004724 Aug 17 '15 at 20:37
  • Please inspect the rendered html source to verify what is missing with your Note button? You can use the developer tool of your browser or just right click on the page rendered and select 'View Page Source'. May be you can paste the html you find here. – Sunil Aug 17 '15 at 20:38
  • Thank you Sunil, this is what I get from the Page Source: – user5004724 Aug 17 '15 at 20:48
  • I can already see a problem. The onclick attribute will not be recognized by html because of $classStr before it. There is some php mistake you are doing. That is why your Note button is not working. – Sunil Aug 17 '15 at 20:52
  • You are right! I changed that part. Still not working....I will paste new parts in the question. – user5004724 Aug 17 '15 at 21:01
  • Thank you Sunil, I will check the whole code and let you know. – user5004724 Aug 17 '15 at 21:05