最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to I make a radio button which switches background color in bootstrap and js? - Stack Overflow

programmeradmin4浏览0评论

I'd like to make a idea note like bulletin board to use bootstrap and Javascript. So I make a input-group include send button, enter or click message add like a stack. But I don't know how to use when onclick two function which is setting background-color and send message at the same time. In radio button, when I check the button, Can I change the color code in bootstrap?

The radio button which switch card-header's background color Pink or Gray can change card-header's color when I checked color and enter the message. Depending on the checked radio button, the color of the header part where the message was pressed must be changed.

function addMessage() {
  const input = document.getElementById('messageInput');
  const messageText = input.value.trim();
  if (messageText !== '') {
    const messageHTML = `
<div class="card mb-3 shadow-sm">
  <div class="card-header bg-primary">
    <div class="text-muted">
      Posted by ${getCurrentUser()} at ${formatDateTime()}
    </div>
  </div>
  <div class="card-body">
    <div class="d-flex justify-content-between align-items-top">
      <div class="flex-grow-1">
        <div class="message-text">${messageText}</div>
      </div>
      <button class="btn btn-danger btn-sm ms-2" onclick="this.closest('.card').remove()">Delete</button>
    </div>
  </div>
</div>`;

  }
}
<!-- Input group -->
<div class="d-flex justify-content mb-5">
  <div class="input-group">
    <input type="text" class="form-control" id="messageInput" placeholder="Text input">
    <button type="button" class="btn btn-primary" onclick='addMessage()'>Send</button>
  </div>
</div>

<div class="message-board" id="messageBoard"></div>

I'd like to make a idea note like bulletin board to use bootstrap and Javascript. So I make a input-group include send button, enter or click message add like a stack. But I don't know how to use when onclick two function which is setting background-color and send message at the same time. In radio button, when I check the button, Can I change the color code in bootstrap?

The radio button which switch card-header's background color Pink or Gray can change card-header's color when I checked color and enter the message. Depending on the checked radio button, the color of the header part where the message was pressed must be changed.

function addMessage() {
  const input = document.getElementById('messageInput');
  const messageText = input.value.trim();
  if (messageText !== '') {
    const messageHTML = `
<div class="card mb-3 shadow-sm">
  <div class="card-header bg-primary">
    <div class="text-muted">
      Posted by ${getCurrentUser()} at ${formatDateTime()}
    </div>
  </div>
  <div class="card-body">
    <div class="d-flex justify-content-between align-items-top">
      <div class="flex-grow-1">
        <div class="message-text">${messageText}</div>
      </div>
      <button class="btn btn-danger btn-sm ms-2" onclick="this.closest('.card').remove()">Delete</button>
    </div>
  </div>
</div>`;

  }
}
<!-- Input group -->
<div class="d-flex justify-content mb-5">
  <div class="input-group">
    <input type="text" class="form-control" id="messageInput" placeholder="Text input">
    <button type="button" class="btn btn-primary" onclick='addMessage()'>Send</button>
  </div>
</div>

<div class="message-board" id="messageBoard"></div>

Share Improve this question edited Jan 19 at 13:11 mplungjan 178k28 gold badges180 silver badges240 bronze badges asked Jan 19 at 13:08 KimgeekKimgeek 31 bronze badge 1
  • I made a snippet. Please complete it. It was missing the end of the script and two end divs – mplungjan Commented Jan 19 at 13:11
Add a comment  | 

1 Answer 1

Reset to default 0

I would delegate

window.addEventListener('load', () => { // when page has loaded
  const input = document.getElementById('messageInput');
  const messageBoard = document.getElementById('messageBoard');

  const getCurrentUser = () => {
    return "User"; // Replace with your logic to fetch the current user
  };

  const formatDateTime = () => {
    const now = new Date();
    return `${now.toLocaleDateString()} ${now.toLocaleTimeString()}`;
  };

  const addMessage = () => {
    const messageText = input.value.trim();

    // Get selected color
    const selectedColor = document.querySelector('input[name="headerColor"]:checked').value;

    if (messageText !== '') {
      const messageHTML = `
<div class="card mb-3 shadow-sm">
  <div class="card-header ${selectedColor}">
    <div class="text-muted">
      Posted by ${getCurrentUser()} at ${formatDateTime()}
    </div>
  </div>
  <div class="card-body">
    <div class="d-flex justify-content-between align-items-top">
      <div class="flex-grow-1">
        <div class="message-text">${messageText}</div>
      </div>
      <button class="btn btn-danger btn-sm ms-2 delete">Delete</button>
    </div>
  </div>
</div>`;
      messageBoard.insertAdjacentHTML('beforeend', messageHTML);
      input.value = ''; // Clear the input
    } else {
      alert('Please enter a message!');
    }
    input.focus();
    input.select();
  }
  messageBoard.addEventListener('click', (e) => {
    const tgt = e.target.closest('button.delete');
    if (!tgt) return;
    tgt.closest('.card').remove()
  })
  
  document.getElementById('send').addEventListener('click', addMessage);
  input.focus();
  input.select();
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />

<!-- Color Selection Radio Buttons -->
<div class="mb-3">
  <label class="form-label">Choose Header Color:</label>
  <div>
    <input type="radio" id="pink" name="headerColor" value="bg-danger" checked>
    <label for="pink">Pink</label>
    <input type="radio" id="gray" name="headerColor" value="bg-secondary">
    <label for="gray">Gray</label>
  </div>
</div>

<!-- Input group -->
<div class="d-flex justify-content mb-5">
  <div class="input-group">
    <input type="text" class="form-control" id="messageInput" placeholder="Text input">
    <button id="send" type="button" class="btn btn-primary">Send</button>
  </div>
</div>

<!-- Message Board -->
<div class="message-board" id="messageBoard"></div>

发布评论

评论列表(0)

  1. 暂无评论