I want to replace the inp element to span element the code is working fine for 1 time only when I click 2nd time on the check btn value goes undefined

Karan Rajput

I want to replace the inp element to span element the code is working fine for 1 time only when I click 2nd time on the check btn value goes undefined

let text = document.getElementsByClassName('text');
let items = document.querySelectorAll('.items');
let checkBtn = document.querySelector('.check-icon');

// Creating a SPAN element and appending it to div

for (let i = 0; i < text.length; i++) {
  checkBtn.addEventListener('click', () => {
    let span = document.createElement('span');
    let val = document.createTextNode(text[i].value);
    span.appendChild(val);
    span.setAttribute('class', 'text');
    items[i].appendChild(span);
    text[i].value = '' // setting the input value to empty once clicked onto the check button
    text[i].parentNode.replaceChild(span, text[i]);


  })
}
.mainContainer {
  height: 400px;
  width: 900px;
  background-color: white;
  margin: 200px auto;
  border: 5px solid black;
  border-radius: 8px;
}

.heading {
  font-family: 'Roboto', sans-serif;
  font-weight: bold;
  text-align: center;
  position: relative;
  top: 15px;
}

.container {
  width: 800px;
  height: auto;
  border: 2px solid black;
  display: grid;
  grid-template-columns: 230px 230px 230px 50px 50px;
  align-items: center;
  margin: auto;
  position: relative;
  top: 30px;
  padding: 10px;
  background-color: #007bff;
}

.items {
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Roboto', sans-serif;
  font-weight: bold;
  color: #fff;
}

.text {
  width: 130px;
}

.icons {
  font-size: 18px;
  border: 2px solid #fff;
  margin-left: 12px;
  color: #007bff;
  cursor: pointer;
  background-color: #fff;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icons:hover {
  color: #fff;
  background-color: #007bff;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Document</title>
  <link rel="stylesheet" href="style.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body style="background-color: #007bff">
  <div class="mainContainer">
    <h1 class="heading">Details Collector</h1>
    <div class="container">


      <div class="items">
        <label class="label" for="Name">Name :</label> &nbsp;&nbsp;&nbsp;
        <input class="text" type="text" />
      </div>
      <div class="items">
        <label class="label" for="State">State :</label> &nbsp;&nbsp;&nbsp;
        <input class="text" type="text" />
      </div>
      <div class="items">
        <label class="label" for="Country">Country :</label> &nbsp;&nbsp;&nbsp;
        <input class="text" type="text" />
      </div>


      <div class="check-icon icons">
        <i class="fa fa-check" aria-hidden="true"></i>
      </div>


      <div class="plus-icon icons ">
        <i class="fa fa-plus" aria-hidden="true"></i>
      </div>


    </div>
  </div>

  <script src="app.js"></script>
</body>

</html>

mplungjan

Second time around the span does not have a value

const container = document.querySelector(".container");

// Creating a SPAN element and appending it to div
container.addEventListener('click', (e) => {
  const tgt = e.target.closest(".icons");
  if (tgt) {
    if (tgt.classList.contains("swapped")) return; // stop
    if (tgt.classList.contains("check-icon")) {
      tgt.classList.add("swapped")
      let texts = document.querySelectorAll('.text');
      let items = document.querySelectorAll('.items');
      texts.forEach((text, i) => {
        let span = document.createElement('span');
        let val = document.createTextNode(text.value ? text.value : '');
        span.appendChild(val);
        span.classList.add('text');
        items[i].appendChild(span);
        if (text.value) text.value = '' // setting the input value to empty once clicked onto the check button
        text.parentNode.replaceChild(span, text);
      })
    }
  }
})
.mainContainer {
  height: 400px;
  width: 900px;
  background-color: white;
  margin: 200px auto;
  border: 5px solid black;
  border-radius: 8px;
}

.heading {
  font-family: 'Roboto', sans-serif;
  font-weight: bold;
  text-align: center;
  position: relative;
  top: 15px;
}

.container {
  width: 800px;
  height: auto;
  border: 2px solid black;
  display: grid;
  grid-template-columns: 230px 230px 230px 50px 50px;
  align-items: center;
  margin: auto;
  position: relative;
  top: 30px;
  padding: 10px;
  background-color: #007bff;
}

.items {
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Roboto', sans-serif;
  font-weight: bold;
  color: #fff;
}

.text {
  width: 130px;
}

.icons {
  font-size: 18px;
  border: 2px solid #fff;
  margin-left: 12px;
  color: #007bff;
  cursor: pointer;
  background-color: #fff;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icons:hover {
  color: #fff;
  background-color: #007bff;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Document</title>
  <link rel="stylesheet" href="style.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer"
  />
</head>

<body style="background-color: #007bff">
  <div class="mainContainer">
    <h1 class="heading">Details Collector</h1>
    <div class="container">


      <div class="items">
        <label class="label" for="Name">Name :</label> &nbsp;&nbsp;&nbsp;
        <input class="text" type="text" />
      </div>
      <div class="items">
        <label class="label" for="State">State :</label> &nbsp;&nbsp;&nbsp;
        <input class="text" type="text" />
      </div>
      <div class="items">
        <label class="label" for="Country">Country :</label> &nbsp;&nbsp;&nbsp;
        <input class="text" type="text" />
      </div>


      <div class="check-icon icons">
        <i class="fa fa-check" aria-hidden="true"></i>
      </div>


      <div class="plus-icon icons ">
        <i class="fa fa-plus" aria-hidden="true"></i>
      </div>


    </div>
  </div>

  <script src="app.js"></script>
</body>

</html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I want to find an input element closest a span when I click the span, can you help me?

My polylines wont disappear when I click the button the 2nd time

How do I allow only 1 element to be selected at a time within my click function? ( Vanilla JS )

i want to toggle between two functions this code works only one time but i want to make it work each time i click

Text in NestedScrollView in Fragment overlaps the 2nd time that I open the Fragment. First time it works fine

I want to align the text in my list element that contains a time element

How can I change CSS style, and focus on element as time goes by?

Windows old width coming undefined on first resize, but from 2nd time resize its working fine, Vue jS

I want the time to show based on the selection on the 2nd drop down list

circular queue inserting element problem when i try to insert 2nd element it is showing queue is full

I want to check the time interval

HiChart click event 1st time it's working but when I update the data then click event is not working

Quicksort time complexity when it always select the 2nd smallest element as pivot in a sublist

I want to put the effect of the hover only on 1 element however 2 element was applied

Undefined offset: 2 when I delete an element of the array and I want to return the rest of them

I am getting Null pointer exception when i run my app 2nd time

when i click a button second time my label want to dissappear

User will get the dialog when the user first time login to the portal. and i want to check terms & condition checkbox only first time

A request to send or receive data was disallowed because the socket is not connected (only if I connect 2nd time)

Form element goes blank when I dispatchEvent()

RecyclerView onClick() not working, instead of it only responding to when I click a single element

Page extends when I want only a scrollable element

How can I add a value that is incremented every 2nd time to each object of an array?

I want the "show" and "hide button to only target 1 image at a time

Button shoud be all the time clicked even when I'm in 2nd activity

2 audio sounds and I want to play one HTML5 audio element at a time

How do I find the value of 2nd column (td element) of 4th row (tr element) in jquery?

How can i activate .hover() function only on one element at time

In my date time value I want to use regex to strip out the slash and colon from time and replace it with underscore