blue-sky blue-sky - 18 days ago 4x
Node.js Question

Mixing javascript with node.js and pug

I'm attempting to display multiple tabs using code from here

Here is my code :

index.pug :


function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;

// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";

// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");

// Show the current tab, and add an "active" class to the link that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";

title= title
h3= message

<link rel="stylesheet" type="text/css" href="style.css">

<ul class="tab">
<li><a href="#" class="tablinks" onclick="openCity(event, 'London')">London</a></li>
<li><a href="#" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a></li>
<li><a href="#" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a></li>

<div id="London" class="tabcontent">
<p>London is the capital city of England.</p>

<div id="Paris" class="tabcontent">
<p>Paris is the capital of France.</p>

<div id="Tokyo" class="tabcontent">
<p>Tokyo is the capital of Japan.</p>

style.css :

/* Style the list */ {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;

/* Float the list items side by side */ li {float: left;}

/* Style the links inside the list items */ li a {
display: inline-block;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;

/* Change background color of links on hover */ li a:hover {background-color: #ddd;}

/* Create an active/current tablink class */ li a:focus, .active {background-color: #ccc;}

/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;

I receive this error :

9| // Get all elements with class="tabcontent" and hide them
10| tabcontent = document.getElementsByClassName("tabcontent");
> 11| for (i = 0; i < tabcontent.length; i++) {
12| tabcontent[i].style.display = "none";
13| }

malformed each
at makeError (/Users/node_modules/pug/node_modules/pug-lexer/node_modules/pug-error/index.js:32:13)
at Lexer.error (/Users/node_modules/pug/node_modules/pug-lexer/index.js:58:15)
at Lexer.each (/Users/node_modules/pug/node_modules/pug-lexer/index.js:911:12)
at Lexer.callLexerFunction (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1315:23)
at Lexer.advance (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1343:15)
at Lexer.callLexerFunction (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1315:23)
at Lexer.getTokens (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1371:12)
at lex (/Users/node_modules/pug/node_modules/pug-lexer/index.js:12:42)
at Object.load.string.lex (/Users/node_modules/pug/lib/index.js:93:27)
at Function.loadString [as string] (/Users/node_modules/pug/node_modules/pug-load/index.js:44:24)

How to mix javascript with pug ?


There is a small example with inline javascript on the pug npm page ( Basically its this:

        script (type="text/javascript").
            /* your javascript here */
        title= title

Also I'm not sure why you're using partial pug syntax and partial HTML. For example you have this:

<link rel="stylesheet" type="text/css" href="style.css">

When it should be this:

link(rel="stylesheet" type="text/css" href="style.css")

As an alternative (for the javascript) you can use the include directive, as explained here:, or you could put the javascript in an external file (like you did with your css) and simply do this: