-1

I want the button with the id #show-text-area execute the postButton(); function only once so it won't create a second elements whenever clicked (i want it to create it for only one time and won't work again until clicked another button). Hope my question was clear enough.

HTML

 <div id="post-creator" class="creator-container">
    <div class="post-type">
        <div class="text-post" id="post">
            <button onclick="postButton();">Post</button>
        </div>
        <div class="media-post">Image & Video</div>
        <div class="link-post">Link</div>
    </div>
    <div class="post-title">
        <input type="text" class="title-text" name="post-title" placeholder="Title">
    </div>
    <div class="post-content">

    </div>
    <div class="post-footer">
        <div class="spoiler">Spoiler</div>
        <div class="nsfw">NSFW</div>
        <button class="post">post</button>
    </div>
</div>

Javascript

let postButton = function() {
     let textarea = document.createElement('textarea');
    textarea.setAttribute('class', 'post-data');
    textarea.setAttribute('placeholder', 'Text (optional)');
    document.querySelector('.post-content').appendChild(textarea);
}
dns_nx
  • 3,651
  • 4
  • 37
  • 66
Salah Eddine Makdour
  • 1,023
  • 1
  • 12
  • 24

3 Answers3

1

You could disable the button after activation, this has the benefit of informing the user that further clicks won't do anything.

let postButton = function() {
     let textarea = document.createElement('textarea');
    textarea.setAttribute('class', 'post-data');
    textarea.setAttribute('placeholder', 'Text (optional)');
    document.querySelector('.post-content').appendChild(textarea);
    document.getElementsByTagName("button")[0].disabled = true;
}

Otherwise you could simply have the function short-circuit if it has already been called.

// alreadyPosted is scoped outside of the function so it will retain its value
// across calls to postButton()
let alreadyPosted = false;
let postButton = function() {
    // do nothing if this isn't the first call
    if (alreadyPosted) { return; }
    // mark the function as called
    alreadyPosted = true;
    let textarea = document.createElement('textarea');
    textarea.setAttribute('class', 'post-data');
    textarea.setAttribute('placeholder', 'Text (optional)');
    document.querySelector('.post-content').appendChild(textarea);
    document.getElementsByTagName("button")[0].disabled = true;
}
CollinD
  • 7,304
  • 2
  • 22
  • 45
1

The following works.

let postButton = function(event) {
  event.target.disabled = true;
  let textarea = document.createElement('textarea');
  textarea.setAttribute('class', 'post-data');
  textarea.setAttribute('placeholder', 'Text (optional)');
  document.querySelector('.post-content').appendChild(textarea);
};

document.getElementById('post').addEventListener('click', postButton);
<div id="post-creator" class="creator-container">
  <div class="post-type">
    <div class="text-post" id="post">
      <button>Post</button>
    </div>
    <div class="media-post">Image & Video</div>
    <div class="link-post">Link</div>
  </div>
  <div class="post-title">
    <input type="text" class="title-text" name="post-title" placeholder="Title">
  </div>
  <div class="post-content">

  </div>
  <div class="post-footer">
    <div class="spoiler">Spoiler</div>
    <div class="nsfw">NSFW</div>
    <button class="post">post</button>
  </div>
</div>
GregL
  • 37,147
  • 8
  • 62
  • 67
0

You can also use hide show function on textarea if you do not want to create one.

let postButton = function() {
    let d = document.getElementById('post_data').style.display;
    if(d=='none'){
      document.getElementById('post_data').style.display = 'block';
    }
}
document.getElementById('post_data').style.display = 'none';
document.getElementById('post_btn').addEventListener('click', postButton);
<div id="post-creator" class="creator-container">
  <div class="post-type">
    <div class="text-post">
      <button id="post_btn">Post</button>
    </div>
    <div class="media-post">Image &amp; Video</div>
    <div class="link-post">Link</div>
  </div>
  <div class="post-title">
    <input type="text" class="title-text" name="post-title" placeholder="Title">
  </div>
  <div class="post-content">
   <textarea class="post-data" id="post_data" placeholder="Text (optional)"></textarea>   
   </div>
  <div class="post-footer">
    <div class="spoiler">Spoiler</div>
    <div class="nsfw">NSFW</div>
    <button class="post">post</button>
  </div>
</div>
Haritsinh Gohil
  • 5,818
  • 48
  • 50