No al cierre de webs
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1 let editIndex = -1;2 3 function showVariableInput(name = '', value = '', index = -1) {4  document.getElementById('variable-name').value = name;5  document.getElementById('variable-value').value = value;6  document.getElementById('variable-input').classList.remove('hidden');7  editIndex = index;8 }9 10 function hideVariableInput() {11  document.getElementById('variable-input').classList.add('hidden');12  document.getElementById('variable-name').value = '';13  document.getElementById('variable-value').value = '';14  editIndex = -1;15 }16 17 function saveVariable() {18  const name = document.getElementById('variable-name').value;19  const value = document.getElementById('variable-value').value;20  const list = document.getElementById('variables-list');21 22  if (name && value) {23  if (editIndex === -1) {24  // Add new variable25  const li = document.createElement('li');26  li.classList.add('p-2', 'bg-gray-100', 'border', 'border-gray-300', 'rounded', 'flex', 'justify-between', 'items-center');27  li.innerHTML = `28  <span>${name} = ${value}</span>29  <div>30  <button onclick="editVariable(${list.children.length}, '${name}', '${value}')" class="text-blue-500 mr-2">Edit</button>31  <button onclick="removeVariable(this)" class="text-red-500">x</button>32  </div>`;33  list.appendChild(li);34  } else {35  // Edit existing variable36  const li = list.children[editIndex];37  li.innerHTML = `38  <span>${name} = ${value}</span>39  <div>40  <button onclick="editVariable(${editIndex}, '${name}', '${value}')" class="text-blue-500 mr-2">Edit</button>41  <button onclick="removeVariable(this)" class="text-red-500">x</button>42  </div>`;43  }44 45  hideVariableInput();46  }47 }48 49 function editVariable(index, name, value) {50  showVariableInput(name, value, index);51 }52 53 function removeVariable(element) {54  element.parentElement.parentElement.remove();55 }56 57 function saveTest() {58  alert('Test saved!');59 }60 
Enlace
El enlace para compartir es: