Search code examples
jqueryimagesrcurl-encoding

Jquery to swap img src with a string? + url encoding


I am working with a qr code generator and I need the image (the generate qr code) to swap out when the user hits the 'generate code' button. So I need the image src to become both the textarea value and the error correction value combined, as well as url encoded.

Here is what I have:

    <script type="text/javascript">
$(document).ready(function() {
    var $qr_image = $('#qr-image');
    var $qr_text = $('#qr-text');
    var $qr_elc = $('#qr-elc');

    $.qrGenerate = function() {
        $qr_image.attr('src', '$qr_text.val' + '$qr_elc.val');
    }
    });
</script>

the line below is where my troubles come from. Right now when I click the button I get: "http://urlblahblah/$qr_text.val$qr_elc.val"

$qr_image.attr('src', '$qr_text.val' + '$qr_elc.val');

I also know the encoding is done by using escape('');, but I am not sure where to put this. Thanks for the help!


Solution

  • when you put quotes around a variable it becomes a string, you should use variable name without quotation marks:

    $.qrGenerate = function() {
        $qr_image.attr('src', $qr_text.val() + $qr_elc.val());
    }