Search code examples
javascripturldocumentsrc

replace two document.write values from two inputs


the javascript below replaces this value http://www.google.co.uk/webhp?hl=en with http://link-ads.blogspot.com/?url=http%3A%2F%2Fwww.google.co.uk%2Fwebhp%3Fhl%3Den on input entry.

<script type="text/javascript">
function showMe(e) {
paramencode = encodeURIComponent(e.value)
document.write('http://link-ads.blogspot.com/?url='+paramencode+'');
}
</script>
<input type="text" id="foo" placeholder="URL here" onchange="showMe(this)" />

It works fine but I now need to inclue an &name= + value inherited from second input to the end of the document write ie.

document.write('http://link-ads.blogspot.com/?url='+paramencode+'&name='+andnameinput+'');

where '+andnameinput+' will inherit the values from a second <input>

EDITED if i insert http://www.google.co.uk/webhp?hl=en into the first input

<input type="text" id="foo" placeholder="URL here"/>

the output from the document.write should be http://link-ads.blogspot.com/?url=http%3A%2F%2Fwww.google.co.uk%2Fwebhp%3Fhl%3Den

but it is incomplete as it should have &name= infront with a user input value for that i need a second input

<input type="text" id="foo2" placeholder="Name here"/>

and say if the value 123456789 was input the output returned from the document.write should be http://link-ads.blogspot.com/?url=http%3A%2F%2Fwww.google.co.uk%2Fwebhp%3Fhl%3Den&name=123456789


Solution

  • If i understand well you want something like that:

    <script type="text/javascript">
        function showMe(e) {
            paramencode = encodeURIComponent(e.value)
            document.getElementById('urlEncoded').innerHTML = document.getElementById('urlEncoded').innerHTML + 'http://link-ads.blogspot.com/?url='+paramencode;
        }
        function UpdateParam() {
            document.getElementById('urlEncoded').innerHTML = document.getElementById('urlEncoded').innerHTML + '&name='+ document.getElementById('foo2').value;
        }
    </script>
    <div id="urlEncoded"></div>
    <input type="text" id="foo" placeholder="URL here" onchange="showMe(this)" />
    <input type="text" id="foo2" placeholder="SecondParam" onchange="UpdateParam()" />