Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Alert showing when it shouldnt be (XML Parse)

 <script>
      async function loadData() {
        var data = await fetch("Product.xml");
        var parsedData = await data.text();

        var parser = new DOMParser();
        var Product_document = parser.parseFromString(parsedData,"text/xml");
        var results = "";
        var AlertBox = ""
        var user_id_input = document.getElementById("user_id").value;
        var todos = Product_document.getElementsByTagName("product");
        for(var i = 0; i < todos.length; i++) {
          var Name = todos[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue;
          var Code = todos[i].getElementsByTagName("Code")[0].childNodes[0].nodeValue;
          var Quantity = todos[i].getElementsByTagName("Quantity")[0].childNodes[0].nodeValue;
          var Description = todos[i].getElementsByTagName("Description")[0].childNodes[0].nodeValue;
          var Price = todos[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue;
          if(user_id_input === Code) {
            results = "<div>"
            + "Code: " + Code 
            + ",<br/> Name: " + Name 
            + ", <br/>Quantity: " +  Quantity 
            + ",<br/> Description  " +  Description 
            + ",<br/> Price " +  Price
            + "</div><br/>";
            AlertBox= "True";
          }
         if(AlertBox !== "True") {
             alert("Error");
         }
        }
        document.getElementById("results").innerHTML = results;
      }
    </script>
  

I'm trying to code a web app that takes user input, parses an XML file and then displays some information. I have that part working.

My problem is, I want there to be an Error alert if the Input does not match any of the XML elements. I have coded one in, but for every element the app checks that doesn't match the user input the app is giving me an error alert. And I have no idea how to solve it. enter image description here

I've tried adding a variable that changes to true if the input matches and only allowing the alert to show up if that variable is false and I still get the Alert. enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Based on your code I think the reason why it is not working is the nodeValue property as this returns null.

When called on an element tag (nodeType 1) nodeValue returns null. nodeValue will return the actual text content when it is called from a text element (nodeType 3). The 'Code' tag is of type element and has a child of type text therefore you don't get the desired text value. You can read more on nodeType here.

To get the correct result from nodeValue you need to call it like this:

var Name = todos[i].getElementsByTagName("Name")[0].childNodes[0].firstChild.nodeValue;.

As you want to get the text content from an element you can also use the following code instead:

var Name = todos[i].getElementsByTagName("Name")[0].childNodes[0].textContent;

The if condition should now also be executed correctly as long as the user_id_input variable has also the correct value. If the function loadData is executed before the user has entered values into the form fields, you need to execute the function when your form has been submitted. This way you can make sure that the user has entered values to all required fields. You can read more about HTML Forms and managing them via JavaScript here if needed.

I would also suggest to use let and const instead of var. Additionally I think you would like to use your variable AlertBox as a boolean. If that is the case please use the boolean datatype in JavaScript. You can write it like this:

let AlertBox = false; or let AlertBox = true;

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!