1

I turned the input text-value into the inp1 var but once I want to call it inside the obj it doesn't work.

<input type="text" placeholder="Enter your text here" class="input fade-in" id="inp1" required>
 var inp1 = document.getElementById("inp1").value;

$(function(){
  $('#qrcode').qrcode({
    width: 150,
    height: 150,
    text: "https://www.stackoverflow.com/" + inp1
  });
});

I expect the qrcode code to show the url + the text of the input

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28

1 Answers1

3

Your code runs once, when the page loads. At that time, the input field is yet empty. Instead you probably want to update the qr code whenever the input changes. You need an event listener for that:

 $(function(){
   var input = $("#inp1"); // if you use jQuery, use it everywhere. Also retrieve the element when the document loaded

   input.on("change", function() { // listen for input changes
     $('#qrcode').qrcode({ // then update the qr code
       width: 150,
       height: 150,
       text: "https://www.stackoverflow.com/" + input.val(),
     });
   });
 });

You might want to consider using the input event instead of the change event depending on your usecase.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • The weird thing is that the [library page](https://davidshimjs.github.io/qrcodejs/) has exactly the same example the OP is looking for – Alon Eitan Jul 06 '19 at 17:20
  • It's not uncommon for documentation to have errors in it. – Scott Marcus Jul 06 '19 at 17:23
  • @ScottMarcus That example is working just fine and updating the qrcode as expected (I mean, I didn't actually scan it, but I saw it was changing) – Alon Eitan Jul 06 '19 at 17:24
  • 1
    @AlonEitan that's not the library that i'm using. That library doesn't work – codeworking Jul 06 '19 at 17:26
  • Why does it give me this error: Uncaught SyntaxError: missing ) after argument list – codeworking Jul 06 '19 at 17:37
  • @codeworking Is it [this library](https://github.com/jeromeetienne/jquery-qrcode) then? Because if so, when you run `$('#qrcode').qrcode()` multiple times it adds a new qrcode and not replacing the old one. If it happens to you then you should change the code to `$('#qrcode').empty().qrcode({ .... });` (Note the `empty()` I added) – Alon Eitan Jul 06 '19 at 17:37
  • @AlonEitan you can find the library here https://codepen.io/rizopoulos/pen/KWjmQx – codeworking Jul 06 '19 at 17:44
  • @codeworking Yep, so it has that problem that I was talking about, check [this fiddle](https://codepen.io/anon/pen/KjxYjG) You can see 2 qrcodes in the first wrapper and only one in the second one, because I added `empty()` – Alon Eitan Jul 06 '19 at 17:47
  • @AlonEitan alright it works! So basically it's the jquery.qrcode.js library. Where does the documentation talk about the empty() ? – codeworking Jul 06 '19 at 18:11
  • @codeworking Nowhere, I just checked that library and noticed it, so I thought I will let you know in advance – Alon Eitan Jul 06 '19 at 18:13
  • Alright, thanks to both Alon and Jonas for your patience – codeworking Jul 06 '19 at 18:22