Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript How to add some tags by default

I want to some tags by default witch show up time of open the page. This code normally working fine as add tags but when I trying some tags default such as java, html, php etc then code normally working but no show up default tags. Below is my code, please advice me how to do

Code:
const removeTag = (event) => {
    if (event.target.classList.contains('tag-close')) {
      event.target.parentElement.remove();
    }
  }
 
  const addTag = (event) => {
    if (event.keyCode === 13) {
      const input = document.getElementById('input')
      const tagsContainer = document.querySelector('.tags-container');     
      const value = event.target.value;
      const spanElement = document.createElement('span');
      
 
      spanElement.innerHTML = `
        <span class="tag-text">${value}</span>
        <span class="tag-close"> ⌫ </span>
      `   

      spanElement.classList.add('tag');           
      tagsContainer.appendChild(spanElement);
      input.value = ''; 
      tag = ["coding", "java"];     
    }
  }
  
  window.onload = () => {
    const tagsContainer = document.querySelector('.tags-container');
    tagsContainer.addEventListener('click', removeTag);
  }

I can't do it when I trying. I added it like tag = ["coding", "java"]; but no result I got. How to include by some tags default? I am not much experience in the field. I will be grateful if anybody help me.
 
So you mean something like this?
HTML:
<span class="tag1"></span>
And then the classes tag2 and tag3 can be added optionally using javascript?
 
Something like this:
HTML:
<!-- tags. tag1 is the default tag -->
<span id="tags" class="tag1"></span>

<!-- buttons to add tags -->
<button onclick="addTag('tag2')">Add tag2</button>
<button onclick="addTag('tag3')">Add tag3</button>
<button onclick="addTag('tag4')">Add tag4</button>
JavaScript:
function addTag(tag) {
  document.querySelector("#tags").classList.add(tag);
}
 
Something like this:
HTML:
<!-- tags. tag1 is the default tag -->
<span id="tags" class="tag1"></span>

<!-- buttons to add tags -->
<button onclick="addTag('tag2')">Add tag2</button>
<button onclick="addTag('tag3')">Add tag3</button>
<button onclick="addTag('tag4')">Add tag4</button>
JavaScript:
function addTag(tag) {
  document.querySelector("#tags").classList.add(tag);
}
Thanks for reply, its show as tags but its not removeable. I want if user wants to remove then it will be remove.
 
Thanks for reply, its show as tags but its not removeable. I want if user wants to remove then it will be remove.
What you could do, is create a function that check if the class is present, if so remove it, otherwise add it. Then in your event handling, you call that function:

JavaScript:
function toggleClass(){
    //grab buttons
    if button.contains class
        remove class
    else
        add class 
}
 
What you could do, is create a function that check if the class is present, if so remove it, otherwise add it. Then in your event handling, you call that function:

JavaScript:
function toggleClass(){
    //grab buttons
    if button.contains class
        remove class
    else
        add class
}
Sorry not work, below is my html code:
Code:
 <div class="container">
    <div class="input-container">
      <input type="text" placeholder="Type in something..." id="input" onkeyup="addTag(event)" />
    </div>
    <div class="tags-container">     
      

    </div>
</div>
 
Sorry not work, below is my html code:
Code:
 <div class="container">
    <div class="input-container">
      <input type="text" placeholder="Type in something..." id="input" onkeyup="addTag(event)" />
    </div>
    <div class="tags-container">    
     

    </div>
</div>
That is because I pseudo coded it. I am giving you the logic of how it would be, ot is your responsibility as the coder, as the creator, to attempt to write the code for it, based on the logic.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom