![]() |
#1 |
Junior Member
![]() Posts: 4
Karma: 10
Join Date: Jun 2020
Device: ipad
|
Javascript error in sigil
I have to build an interactive ebook for a class. In that there are tables that I like to hide and show columns based on user response. There plent of examples using jquery, etc to do this. But these methods relay on jquery being accessed by internet. This ebook should function without internet too.
An example using jquery worked in sigil. The following example javascript only gives an error on "for" loop construction of the code when put in sigil. What am I doing wrong. This code is completely functional in all the browsers. Thanks for your help Code:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <table> <thead> <th id="column-a">1</th> <th id="column-b">2</th> <th id="column-c">3</th> <th id="column-d">4</th> <th id="myDIV" style="display:none;">5</th> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td id="myDIV" style="display:none;">5</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td id="myDIV" style="display:none;">5</td> </tr> </tbody> </table> <p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p> <button onclick="myFunction()">Try it</button> <div id="myDIV" style="display:none;"> This is my DIV element. </div> <p><b>Note:</b> The element will not take up any space when the display property set to "none".</p> <script type="text/javascript"> function myFunction() { var elms = document.querySelectorAll("#myDIV"); for (var i = 0; i < elms.length; i++) { if (elms[i].style.display === "none") { elms[i].style.display = "block"; } else { elms[i].style.display = "none"; } } } </script> </body> </html> |
![]() |
![]() |
![]() |
#2 |
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 8,478
Karma: 5703586
Join Date: Nov 2009
Device: many
|
Try using the Inspector in Preview to see the java console and debug your script. Also id attributes must be *unique* so instead your code is probably finding the wrong element and failing.
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,828
Karma: 168802811
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
When I ran epubcheck on your code sample, it complained about multiple errors such as the naked <html> tag, the lack for <tr></tr> tags between the <thead></thead> tags. Then it was unhappy with your javascript.
You might want to check Progressive Enhancement on daisy.org. The sample from there is in the spoiler/code block below and does work in Sigil's preview though not in PageEdit. Spoiler:
|
![]() |
![]() |
![]() |
#4 |
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 8,478
Karma: 5703586
Join Date: Nov 2009
Device: many
|
Did you enable Javascript in PageEdit preferences?
|
![]() |
![]() |
![]() |
#5 |
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,828
Karma: 168802811
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
|
![]() |
![]() |
Advert | |
|
![]() |
#6 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 5,680
Karma: 23983815
Join Date: Dec 2010
Device: Kindle PW2
|
Quote:
Spoiler:
Of course, as KevinH has already pointed out, your book will be invalid because all ids need to be unique. You might want to select the text that you want to hide via class attributes. Last edited by Doitsu; 06-11-2020 at 12:09 PM. |
|
![]() |
![]() |
![]() |
#7 |
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 8,478
Karma: 5703586
Join Date: Nov 2009
Device: many
|
Yes, the "<" sign in the for loop is the culprit. As Doitsu explain using CDATA is a solution. As is including the script from a separate javascript resource.
You can wrap javascript // comment indicators around the CDATA piece as well for some browsers and e-readers ala: Code:
//<![CDATA[ //]]> |
![]() |
![]() |
![]() |
#8 | |
Junior Member
![]() Posts: 4
Karma: 10
Join Date: Jun 2020
Device: ipad
|
Quote:
Thanks for your reply. May be I did not explain myself well. I know about id must be unique, even with this error it works on all browsers, when I put this code to sigil, it complain that an error detected around or above "for". I figured it , it's the "for" statement itself that it complains about, but I do not see why. Bid |
|
![]() |
![]() |
![]() |
#9 |
Junior Member
![]() Posts: 4
Karma: 10
Join Date: Jun 2020
Device: ipad
|
Thank you all. Yes the CDATA tag solved the problem. Now everything works. Yes, I cleaned the code too. DNSB you are write javascripts does not work within SIGIL but the epub generated works well.
Once again thanks you all so much. Bid |
![]() |
![]() |
![]() |
#10 |
Sigil Developer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 8,478
Karma: 5703586
Join Date: Nov 2009
Device: many
|
Yes they do work within Sigil and PageEdit in an epub3 epub, You just need to enable Javascript in Sigil and PageEdit's Preferences. That is how I tested your code.
|
![]() |
![]() |
![]() |
#11 |
Bibliophagist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 44,828
Karma: 168802811
Join Date: Jul 2010
Location: Vancouver
Device: Kobo Sage, Libra Colour, Lenovo M8 FHD, Paperwhite 4, Tolino epos
|
Not quite right. What I said was the Javascript worked in Sigil but not in PageEdit. And was rightly embarrassed when KevinH asked if I had enabled Javascript in PageEdit as well as Sigil. A Doh! moment.
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Sigil Error Message: Book File Would Not Open in Sigil | fkustaa | Sigil | 9 | 04-27-2025 05:11 AM |
Sigil Error | PapaKilo | Sigil | 27 | 07-04-2016 11:08 AM |
Create a javascript quizz for Epub3 in Sigil | BertrandThibaut | Sigil | 3 | 01-26-2014 09:04 AM |
Sigil error | Alonso Alvarez | Sigil | 8 | 10-12-2013 08:29 PM |
Javascript error on conversion azw4 to PDF | SpecterGT260 | Conversion | 2 | 01-15-2013 10:30 PM |