Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving whitespace in Javascript
    primarykey
    data
    text
    <p>Super newbie at Javascript here. I have a problem with whitespace that I'm hoping someone can help me with.</p> <p>I have a function that looks like this:</p> <pre><code>function createLinks() { var i = 0; tbody = document.getElementsByTagName('tbody')[3]; console.log('test order ID: '+document.getElementsByTagName('tbody')[3].getElementsByTagName('tr')[0].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'')) trs = tbody.getElementsByTagName('tr'); console.log('trs.length = '+trs.length); for (i=0;i&lt;trs.length;i++) { orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,''); console.log('order: '+orderId); hrefReturn = 'https://www.example.com/example.html?view=search&amp;range=all&amp;orderId='+orderId+'+&amp;x=13&amp;y=17'; linkReturn = '&lt;a href='+hrefReturn+'&gt;'+orderId+'&lt;/a&gt;'; console.log(linkReturn); trs[i].getElementsByTagName('td')[1].innerHTML = linkReturn; } } </code></pre> <p>I call this function using another function when the page is initially loaded. This works perfectly.</p> <p>However, I also call this function in another way when data on the page changes. There's a dropdown list that I have an onClick attribute attached to. That onClick event calls the first function, which in turn calls the second function (above). Both of these functions are saved into text variables and appended to the document, as below:</p> <pre><code>var scriptTextLinks = " function createLinksText() { var i = 0; tbody = document.getElementsByTagName('tbody')[3]; console.log('test order ID: '+document.getElementsByTagName('tbody')[3].getElementsByTagName('tr')[0].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'')); trs = tbody.getElementsByTagName('tr'); console.log('trs.length = '+trs.length); for (i=0;i&lt;trs.length;i++) { orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'').replace(/\s/g,''); orderId = orderId.replace(/\s/g,''); console.log('order: '+orderId); hrefReturn = 'https://www.example.com/example.html?view=search&amp;range=all&amp;orderId='+orderId+'+&amp;x=13&amp;y=17'; linkReturn = '&lt;a href='+hrefReturn+'&gt;'+orderId+'&lt;/a&gt;'; console.log(linkReturn); trs[i].getElementsByTagName('td')[1].innerHTML = linkReturn; } console.log('complete'); } " </code></pre> <p>Finally, here is the specific problem. When THIS version of the same function is called by events on the webpage, it fails to properly delete the whitespace, which breaks the link that it's supposed to create.</p> <p>This is the exact problem section of code: </p> <pre><code>orderId = trs[i].getElementsByTagName('td')[1].textContent.replace(/^\s+|\s+$/g,'').replace(/\s/g,''); orderId = orderId.replace(/\s/g,''); console.log('order: '+orderId); </code></pre> <p>So instead of storing the variable like it should, like this:</p> <pre><code>"XXXXXXXXX" </code></pre> <p>It is stored like this:</p> <pre><code>" XXXXXXXXXX " </code></pre> <p>Which, again, kills the link.</p> <p>Can anybody clarify what's going on here, and how I can fix it? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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