Welcome!

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.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Won't Save

notbod

New Coder
My code calls for pasting a data string into input text area. Then you press a button and it puts the data into a more easy to read editable table. Then when I edit some of the data, I press the save button and it is supposed to move the edited text into the output text field, and then i press a "save to file" button. But when i press the first save button to move it to the output text area, it just puts the orginal string in the ouput text area. instead of the edited data. Here is the save function or the code. Can anyone see any errors or have any suggestions? Thanks!



Code:
 function saveChanges() {
      const tableRows = document.getElementById("result").getElementsByTagName("tr");
      let newTSL = document.getElementById("tslInput").value;

      for (const row of tableRows) {
        const title = row.cells[0].textContent;
        const settingsInputs = row.cells[1].getElementsByTagName("input");
        const settings = Array.from(settingsInputs).map(input => input.value.toUpperCase());
        const pattern = new RegExp(`%${title}\\["([0-9A-F]{2}",?)+`, "i");
        const match = pattern.exec(newTSL);

        if (match) {
          const settingsText = settings.join('","');
          newTSL = newTSL.substring(0, match.index) + `%${title}["${settingsText}"` + newTSL.substring(match.index + match[0].length - 1);
        }
      }

      document.getElementById("tslOutput").value = newTSL;
    }
 
It could be just me being thick but I find your problem description very cryptic. Apart from that, it is impossible to judge a snippet of JS outside of its context.
Please post complete code, instructions how to reproduce the problem, and a description of the intended behavior.
BTW, have you checked the debugger Console for errors ? And are you 100% sure your HTML is error-free ?
 
My code calls for pasting a data string into input text area. Then you press a button and it puts the data into a more easy to read editable table. Then when I edit some of the data, I press the save button and it is supposed to move the edited text into the output text field, and then i press a "save to file" button. But when i press the first save button to move it to the output text area, it just puts the orginal string in the ouput text area. instead of the edited data.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Extract TSL Information</title>
  <style>
    table, th, td {
      border: 1px solid black;
      border-collapse: collapse;
    }
    th, td {
      padding: 5px;
      text-align: left;
    }
    .patch-name {
      color: blue;
    }
  </style>
</head>
<body>
  <h1 class="patch-name">Patch Name</h1>
  <h1>Extract TSL Information</h1>
  <textarea id="tslInput" rows="10" cols="100"></textarea>
  <button onclick="processTSL()">Process TSL</button>
  <table id="result">
    <thead>
      <tr>
        <th>Title</th>
        <th>Settings</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
  <button onclick="saveChanges()">Save Changes</button>
  <textarea id="tslOutput" rows="10" cols="100"></textarea>
  <button onclick="downloadTSL()">Download TSL</button>

  <script>
    function processTSL() {
      const tslInput = document.getElementById("tslInput").value;
      const result = extractVariableTitlesAndSettings(tslInput);
      displayResult(result);
      displayPatchName(tslInput);
    }

    function saveChanges() {
      const tableRows = document.getElementById("result").getElementsByTagName("tr");
      let newTSL = document.getElementById("tslInput").value;

      for (const row of tableRows) {
        const title = row.cells[0].textContent;
        const settingsInputs = row.cells[1].getElementsByTagName("input");
        const settings = Array.from(settingsInputs).map(input => input.value.toUpperCase());
        const pattern = new RegExp(`%${title}\\["([0-9A-F]{2}",?)+`, "i");
        const match = pattern.exec(newTSL);

        if (match) {
          const settingsText = settings.join('","');
          newTSL = newTSL.substring(0, match.index) + `%${title}["${settingsText}"` + newTSL.substring(match.index + match[0].length - 1);
        }
      }

      document.getElementById("tslOutput").value = newTSL;
    }

    function displayResult(result) {
      const tbody = document.getElementById("result").getElementsByTagName("tbody")[0];
      tbody.innerHTML = "";

      for (const variable of result) {
        const row = document.createElement("tr");

        const titleCell = document.createElement("td");
        titleCell.textContent = variable.title;
        row.appendChild(titleCell);

        const settingsCell = document.createElement("td");
        settingsCell.innerHTML = variable.settings.map(setting => `<input type="text" value="${setting}" size="2">`).join(", ");
        row.appendChild(settingsCell);

        tbody.appendChild(row);
      }
    }

    function extractVariableTitlesAndSettings(tsl) {
      const regex = /%([a-z]+(\([0-9]+\))?)/gi;
      let match;
      let variables = [];
      while ((match = regex.exec(tsl)) !== null) {
        const variableTitle = match[1].toUpperCase();
        const settingsStartIndex = regex.lastIndex;
        const settingsEndIndex = tsl.indexOf('"]', settingsStartIndex);
                const settings = tsl.substring(settingsStartIndex + 2, settingsEndIndex).split('","');
        variables.push({ title: variableTitle, settings: settings });
      }
      return variables;
    }

    function displayPatchName(tslInput) {
      const nameRegex = /"name":"(.*?)"/;
      const match = nameRegex.exec(tslInput);
      if (match) {
        document.querySelector(".patch-name").textContent = match[1];
      } else {
        document.querySelector(".patch-name").textContent = "Patch Name Not Found";
      }
    }

    function downloadTSL() {
      const tslOutput = document.getElementById("tslOutput").value;
      if (!tslOutput) {
        alert("Please save changes before downloading.");
        return;
      }

      const blob = new Blob([tslOutput], { type: "text/plain;charset=utf-8" });
      const url = URL.createObjectURL(blob);
      const link = document.createElement("a");
      link.href = url;
      link.download = "tsl_output.tsl";
      link.style.display = "none";

      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  </script>
</body>
</html>
 
Yeah that looks like complete code alright. Now what about instructions and a comprehensible description, rather than just repeating yourself, if it's not too much effort ?
 

Latest posts

Buy us a coffee!

Back
Top Bottom