Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a couple of issue within the script that would prevent the <code>if</code> statement executing correctly.</p> <ol> <li>The boolean and operator in JavaScript is <code>&amp;&amp;</code> not <code>and</code>. Note the if you use <code>&amp;</code> this would be a bitwise and.</li> <li><p>The tests should not be a string. Due to JavaScript type coersion it a string will be converted to a boolean. A <code>null</code> or empty string <code>''</code> will evaluate <code>false</code>, all other strings will evaluate <code>true</code>. Currently you have</p> <pre><code>if("test1 and test2") </code></pre> <p>This should be</p> <pre><code>if(test1 &amp;&amp; test2) </code></pre></li> </ol> <p>So far your updated script would be</p> <pre><code>&lt;script&gt; function getSelectedValue() { if(document.getElementById('type').value==1 &amp;&amp; document.getElementById('cablegroup5').value==9 &amp;&amp; document.getElementById('cablegroup3').value==22 &amp;&amp; document.getElementById('cablelength').value == 11) { // Process Xml } } &lt;/script&gt; </code></pre> <p>You need to use the browser xml parser to handle your xml. I will assume that you have an xml string, if you have a document object then you will have to change the following slightly, this is from <a href="http://www.w3schools.com/Xml/xml_parser.asp" rel="nofollow">w3schools</a>.</p> <pre><code> var xmlString = "&lt;Products&gt;" + "&lt;Product partnumber='foo'&gt;This is product 1&lt;/Product&gt;" + "&lt;Product partnumber='bar'&gt;This is product 2&lt;/Product&gt;" + "&lt;/Products&gt;"; // Load into an XML document var myDoc; if (window.DOMParser) { var parser=new DOMParser(); myDoc=parser.parseFromString(xmlString,"text/xml"); } else // Internet Explorer { myDoc=new ActiveXObject("Microsoft.XMLDOM"); myDoc.async="false"; myDoc.loadXML(xmlString); } // Get all product nodes var products = myDoc.getElementsByTagName('Product'); var i, targetProduct, partNumber; for(i = 0; i &lt; products.length; i += 1){ // Get the partnumber attribute partnumber = products[i].attributes.getNamedItem('partnumber'); // Ensure that the partnumber exists and its value is what is wanted if(partnumber &amp;&amp; partnumber.value == 'foo'){ targetProduct = products[i]; // Exit for break; } } // If the product has been found alert its value. if(targetProduct != null){ alert(targetProduct.textContent || targetProduct.text); } </code></pre> <p>If you were selected a node by id then you could use <code>xmlDoc.getElementById</code> instead of iterating through all nodes of a type and checking attributes.</p> <p>To select the text value of an xml node most browsers use the property <code>textContent</code> although Internet Explorer uses <code>text</code>. The line</p> <pre><code> targetProduct.textContent || targetProduct.text </code></pre> <p>returns <code>textContent</code> if it is present and not null, or the value of <code>text</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload