Could anyone explain why my JS doesn't work
<p>I've been working with some javascript written by someone else, trying to get it to work. I have finally got a solution, but I can't understand why it works, when what I was trying previously doesn't.</p>

<p>Version of function that works:</p>

<pre><code>function pasteIt() {
var pasteString="";
var isfirst = true;

if (pasteBox.length!=null){
    for (var i=0; i &lt; pasteBox.length; i++){
        if (pasteBox[i].checked){
            if (isfirst == true) {
                pasteString = pasteString + pasteBox[i].value;
                isfirst = false;
            } else {
                pasteString = pasteString + pasteBox[i].value;
            }
        }
    }
} else if (pasteBox.checked){
    pasteString = pasteBox.value;
} 

var title = document.getElementById("title" + pasteString).value;
var pub = document.getElementById("pub" + pasteString).value;

if (document.getElementById("sub" + pasteString)==null) {
    var sub = "";
    window.opener.document.edit_form.EI2.value= title + sub;
    window.opener.document.edit_form.EI3.value= pub;
} else {
    var sub = document.getElementById("sub" + pasteString).value;
    window.opener.document.edit_form.EI2.value= title + " : " + sub;
    window.opener.document.edit_form.EI3.value= pub;
}
}
</code></pre>

<p>Version that doesn't work:</p>

<pre><code>function pasteIt() {
var pasteString="";
var isfirst = true;

if (pasteBox.length!=null){
    for (var i=0; i &lt; pasteBox.length; i++){
        if (pasteBox[i].checked){
            if (isfirst == true) {
                pasteString = pasteString + pasteBox[i].value;
                isfirst = false;
            } else {
                pasteString = pasteString + pasteBox[i].value;
            }
        }
    }
} else if (pasteBox.checked){
    pasteString = pasteBox.value;
} 

var title = document.getElementById("title" + pasteString).value;
      var sub = document.getElementById("sub" + pasteString).value;
var pub = document.getElementById("pub" + pasteString).value;


if (document.getElementById("sub" + pasteString)==null) {
    window.opener.document.edit_form.EI2.value= title;
    window.opener.document.edit_form.EI3.value= pub;
} else {

    window.opener.document.edit_form.EI2.value= title + " : " + sub;
    window.opener.document.edit_form.EI3.value= pub;
}
}
</code></pre>

<p>By way of explanation as to what this function does. Basically it's on a page that sometimes does and sometimes doesn't contain a tag with an id="sub" attribute. The function is called when a button is clicked on the page, and this pastes the values of the title, sub, and pub variables into a form on another open page. If the tag with the id="sub" exists on the page then all three variables get sent over to the form, if not only the title and pub variables get pasted (they are always present).</p>

<p>I spent ages figuring out how to get the second function to work before I tried moving the sub variables into the if/else condition, and using an empty string for the sub variable if the sub tag is not present on the page. I just can't see why it wouldn't work with the sub variable declared earlier in the function?</p>

<p>Hope this is enough info!</p>
