How to correctly use <svg> tags with jquery in intellij?

199 views Asked by At

So here I have CDN of both JQuery & jsbarcode -> jsbarcode info https://lindell.me/JsBarcode/ The output should show a barcode the the const "number" var.

  

    <!DOCTYPE html>
     <html lang="en" xmlns:th="http://www.thymeleaf.org"  >

    </head>
    <body >
  
    </div>

         <script>
            const number = '12345678';
            $(document).ready(function(){
                JsBarcode("#barcode", number, {
                    text: number.match(/.{1,4}/g).join("  "),
                    width: 2,
                    height: 50,
                    fontSize: 15,
                });
            });
        </script>

        <svg id="barcode"></svg>



    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/jsbarcode/3.6.0/JsBarcode.all.min.js"></script>



    </body>



</html>

1

There are 1 answers

3
AudioBubble On BEST ANSWER

You need to add the script tags for your libraries before the code that uses them. They can go within the <head> tag. Your code snippet becomes the following:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/jsbarcode/3.6.0/JsBarcode.all.min.js"></script>
</head>

<body>
<script>
  const number = '12345678';
  $(document).ready(function(){
      JsBarcode("#barcode", number, {
          text: number.match(/.{1,4}/g).join("  "),
          width: 2,
          height: 50,
          fontSize: 15,
      });
  });
</script>
<svg id="barcode"></svg>
</body>
</html>

(In the code snippet I provided I also cleaned up some errors in the HTML, like the </div> for a nonexistent div tag.)