0

What I want is to call the showPopup('text goes here'); function and the below block of code to be shown with my text in it.

How can I achieve this?

function showPopup(data) {

}


    <a class="fragment" href="#">
<span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span>
  <div class="content">
    // I WANT TO PUT MY TEXT HERE
  </div>
    </a>
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • You need to append the HTML into the body and then style it with CSS. You could put some timeout on it so the message dissappears after some period. – com2ghz May 12 '15 at 14:38
  • possible duplicate of [How to append data to div using javascript?](http://stackoverflow.com/questions/5677799/how-to-append-data-to-div-using-javascript) – Seth McClaine May 12 '15 at 14:55

2 Answers2

0

JSFiddle example: http://jsfiddle.net/tb5musm6/

function showPopup(data) {
    var elem = document.getElementsByClassName('content');
    elem[0].innerHTML = data;
}

showPopup('asdf');

document.getElementsByClassName will return an array of elements with the given classname

innerHTML variable holds all content between tags

Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • Hi, can you please tell me how to have the div hidden and appeared only when the function is called ? – EnexoOnoma May 13 '15 at 13:46
  • If you're working with the same element, set `.content{ display:none }` in css. Then add `elem[0].style.display='initial';` to the end of the js function (JSFiddle: http://jsfiddle.net/tb5musm6/1/) – Seth McClaine May 13 '15 at 13:51
  • Thank you I changed it a bit to meet my requirements. Maybe it is a quick and dirty fix though.. jsfiddle.net/tb5musm6/2 – EnexoOnoma May 13 '15 at 13:57
  • I have a problem after adding your addition.. if you click on the X button it removes the whole page http://jsfiddle.net/ntqr3ubx/ but didn't happen in here http://jsfiddle.net/xftr5/749/ – EnexoOnoma May 13 '15 at 14:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77725/discussion-between-seth-mcclaine-and-xalloumokkelos). – Seth McClaine May 13 '15 at 14:20
  • No, the first link is my attempt after including your function, and the second link is the example that i took for the modal window. if you click X on the modal window of the second link, the window will be hidden. In the first link, the window will be hidden but also the whole page content – EnexoOnoma May 13 '15 at 14:21
-1

Add an id to you div so you can find it easier.

Then, use innerHTML to modify the content of the div.

<a class="fragment" href="#">
    <span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;'>x</span>
    <div class="content" id="content">
    // I WANT TO PUT MY TEXT HERE
    </div>
</a>

<script>
function showPopup(data) {
    document.getElementById('content').innerHTML(data);
}

showPopup('my text');
</script>
FrancoisBaveye
  • 1,902
  • 1
  • 18
  • 25