Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code looks like you are trying to mix Perl with HTML in a way you would use PHP. <strong>This does not work in Perl.</strong></p> <p>I tried fixing your Perl code first. This will print to your filehandle (which I omitted), but will not not give you the working JavaScript code. I did not change that since I did not understand what you want to do. More on that later.</p> <pre><code>use strict; use warnings; # open of $indexfd filehandle goes here.. # print head of HTML page print $indexfd &lt;&lt;HTML &lt;html&gt; &lt;header&gt; &lt;script type="text/javascript"&gt; function hideit() { var x1 = document.getElementsByTagName("ol").item(0); var x = document.getElementsByTagName("ol"); for (var i = 0; i &lt; x.length; i++) x[i].style.display="none"; } function showit() { var x1 = document.getElementsByTagName("ol").item(0); var x=document.getElementsByTagName("ol"); for (var i = 0; i &lt; x.length; i++) x[i].style.display=""; } &lt;/script&gt; &lt;/header&gt; &lt;body&gt; HTML ; # If I see this correctly, %stats_data looks like this: my %stats_data = ( 'out1' =&gt; { 'key1' =&gt; 'val1', 'key2' =&gt; 'val2', }, 'out2' =&gt; { 'key1' =&gt; 'val1', 'key2' =&gt; 'val2', }, ); my $out = 'out1'; # you need the $out from somewhere # print buttons for each data thingy - you'll want to sort them probably foreach my $key (sort keys %{$stats_data{$out}}) { print $indexfd qq~&lt;input onclick="showit()" type="button" id="$key" value="showit"&gt;&lt;br /&gt;~, qq~&lt;input onclick="hideit()" type="button" id="$key" value="hideit"&gt;&lt;br/&gt;~, # Because $stats_data{$out} is a hash ref you need the -&gt; operator here qq~&lt;ol id="$key"&gt;&lt;li&gt;&lt;b&gt;$stats_data{$out}-&gt;{$key}&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;&lt;br/&gt;~; } print $indexfd qq~&lt;p&gt;more html...&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;~; </code></pre> <p>So there are a few things worth mentioning.</p> <pre><code> print $indexfd ("&lt;input onclick=\"showit()\" type=\"button\" id=\"&lt;?perl echo $key; ?&gt;\" value=\"showit\"&gt;&lt;br&gt;&lt;input onclick=\"hideit()\" type=\"button\" id=\"&lt;?perl echo $key; ?&gt;\" value=\"hideit\"&gt;&lt;br&gt;&lt;b&gt;&lt;ol id=$key&gt;$stats_data{$out}{$key}&lt;ol id=$key&gt; &lt;/b&gt;&lt;br&gt;"); </code></pre> <p>In this rather long line of code you used <code>&lt;?perl echo $key; ?&gt;</code> which looks like php and doesn't work. You also used <code>&lt;ol id=$key&gt;</code> which works because you used double-quotes <code>"..."</code>. Perl substitutes the variables inside the "-delimited string for their values. You don't need that php-like stuff. In order to save yourself the effort of escaping all the double quotes in the HTML code you can use the <code>qq</code>-Operator. See <a href="https://metacpan.org/pod/perlop#Quote-and-Quote-like-Operators" rel="nofollow">perlop</a> for more infos.</p> <p>I explained about the <code>%stats_data</code> data structure in my comments.</p> <p>For printing the large chunk of HTML, I used <a href="https://metacpan.org/pod/perlop#EOF" rel="nofollow">HERE docs</a>.</p> <p>Let's talk about your JavaScript now.</p> <blockquote> <p>Even though the buttons do get created for each <code>$key</code>, clicking on one, controls the <code>$stats_data{$out}{$key}</code> of all.</p> </blockquote> <p>This is because of the way you designed your <code>hideit()</code> and <code>showit()</code> functions. I'm not really ceratin what you want to achieve. If you look at my <code>%stats_data</code> you'll see that there are several keys in 'out1'. That lets the foreach-loop print a set of buttons for each of those keys. The buttons both have the same key as their id, as does the ol. That is not correct. An <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id" rel="nofollow">id-attribute has to be unique</a>.</p> <p>Furthermore, there were some basic issues in your html which I took the liberty to fix as well. If you open an <code>&lt;ol id="foo"&gt;</code> container, you need to close it like <code>&lt;/ol&gt;</code>. Since <code>&lt;ol&gt;</code> is a block-level element, you shouldn't put the inline element <code>&lt;b&gt;</code> outside it, but rather inside the ol's <code>&lt;li&gt;</code> elements (which I omitted). It would be better yet to just assign css ``style="font-weight: bold" to the li items or better yet give them classes.</p> <p>I'll take one last guess at what you were trying to do with the JavaScript. If you have several paragraphs of text and you want to hide them with buttons, you could do that like my untested code here.</p> <p>Javascript:</p> <pre><code>function toggle(id) { if (document.getElementById(id).style.display == 'block' ) { document.getElementById(id).style.display = 'none'; } else { document.getElementById(id).style.display = 'block'; } } </code></pre> <p>HTML:</p> <pre><code>&lt;div&gt; &lt;input type="button" name="toggle1" id="toggle1" onclick="toggle('p1')" /&gt; &lt;p id="p1"&gt;Lorem ipsum dolor set ... foo a lot of text 1.&lt;/p&gt; &lt;input type="button" name="toggle2" id="toggle2" onclick="toggle('p2')" /&gt; &lt;p id="p2"&gt;Lorem ipsum dolor set ... foo a lot of text 2.&lt;/p&gt; &lt;/div&gt; </code></pre> <p>In this case, the function checks whether the paragraph is shown or not. The display-value needs to be set to 'none' or 'block', because the p-element is a block-level element.</p> <p>Please try to post more specific information about the data you use with your script if you need any more help.</p> <p><strong>EDIT:</strong> In the following code I changed the JS functions to both take an id (the key) as a parameter. They only show or hide the lists associated with this key. I changed the button's ids so they're not the same. I also added a div around each pair of buttons and list to make it clearer what belongs where.</p> <pre><code>use strict; use warnings; # open of $indexfd filehandle goes here.. my $indexfd; # print head of HTML page print $indexfd &lt;&lt;HTML &lt;html&gt; &lt;header&gt; &lt;script type="text/javascript"&gt; function hideit(key) { document.getElementById(key).style.display = "none"; } function showit(key) { document.getElementById(key).style.display = ""; } &lt;/script&gt; &lt;/header&gt; &lt;body&gt; HTML ; # If I see this correctly, %stats_data looks like this: my %stats_data = ( 'out1' =&gt; { 'key1' =&gt; 'val1', 'key2' =&gt; 'val2', }, 'out2' =&gt; { 'key1' =&gt; 'val1', 'key2' =&gt; 'val2', }, ); my $out = 'out1'; # you need the $out from somewhere foreach my $key (sort keys %{$stats_data{$out}}) { print $indexfd qq~&lt;div id="div_$key"&gt;\n~, # Add a div around the $key-elements qq~&lt;input onclick="showit('$key')" type="button" id="btn_show_$key" value="showit"&gt;\n~, qq~&lt;input onclick="hideit('$key')" type="button" id="btn_show_$key" value="hideit"&gt;&lt;br/&gt;\n~, qq~&lt;ol id="$key"&gt;&lt;li&gt;&lt;b&gt;$stats_data{$out}-&gt;{$key}&lt;/b&gt;&lt;/li&gt;&lt;/ol&gt;\n~, qq~&lt;/div&gt;\n~; } print $indexfd qq~&lt;p&gt;more html...&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;~; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload