Creating a JavaScript function to change a previously declared variable
===

I am having trouble changing a variable on a a page with lots of PHP and JavaScript.

This part is working fine for me:

    <script language="javascript" type="text/javascript">
    function changebg(my)
    {
    document.getElementById("imglayer").style.backgroundImage ='url('+ my.src +')';
    document.getElementById("cropframe").style.backgroundImage ='url('+ my.src +')';
    }
    var test2 = new String("url('+ my.src +')");
    </script>
    <img src="../../uploads/595MCoDFyArFFe.jpg" width="50" height="50" onclick="changebg(this)" >
    <img src="../../uploads/P6l6J8aqzli6gh.jpg" width="50" height="50" onclick="changebg(this)" >
    <img src="../../uploads/stXWS8fL4L3nvs.jpg" width="50" height="50" onclick="changebg(this)" >

With the div IDs being called for later.

My issue is I have another Variable that I need to change dynamically

    <script type="text/javascript">
    var test1 = new String("<?php $src_name = '");
    var test2 = new String("956ENbXjzTlkBo.jpg");
    var test3 = new String("'; // modify this, original file?>");");
    document.write(String(test1, test2, test3);
    
    </script>

I need the 'var test2' to change with the other two which are being grabbed by the div ID.
How do I change that string value?
Is this even possible?
Any help would be greatly appreciated!