JavaScript
General
- JavaScript or JQuery can be used to program dynamic effects on web pages easily and across browsers or to make web applications more comfortable
- Our websites already contain jQuery
All functions of this JavaScript library can be used - Additional JavaScript code should always be integrated via a separate file (do not use inline code!) – integration is done via variable $javascript in the page header
$javascript[] = 'example/code.js'
- Typical application scenarios include:
- dynamic manipulation of web pages
- plausibility check (data validation) of form input before submission
- Display of dialog boxes and pop-ups
- Sending and receiving data without reloading the page in the browser (Ajax)
- Suggestion of search terms during input
- The possibilities of JavaScript or JQuery are very manifold, therefore use:
- the introduction and some usage examples in the TUCAL documentation
- the official JQuery documentation
Example of use: Survey
Would you like to dive into JavaScript programming in more detail?
Source code for this example
To implement this survey, HTML and JQuery are used:
- HTML Code
<div id="box_start" class="tucbox-tip-info"> <p>Would you like to dive into JavaScript programming in more detail?</p> <p> <button id="script_yes" class="btn btn-default">Yes, let's go!</button> <button id="script_no" class="btn btn-primary">No, not interested</button> </p> </div> <div id="box_yes" class="row" style="display:none;"> <div class="col-md-2"> <img alt="Happy Smiley" src="../bilder/js-yes.png" class="img-responsive script_icon"> </div> <div class="col-md-10"> <h3>Wonderful!</h3> <p>If you want to get started with JavaScript programming, please go this way:</p> <ul> <li><a href="https://api.jquery.com/" class="linkextern">JQuery documentation</a></li> <li><a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript" class="linkextern">introduction in JavaScript</a></li> <li><a href="https://wiki.selfhtml.org/wiki/JavaScript/Tutorials" class="linkextern">JavaScript tutorials</a></li> </ul> <hr> <h3>Restart survey?</h3> <p> <button class="script_startover btn btn-default">Start again</button> <button class="script_stop btn btn-primary">Finish survey</button> </p> </div> </div> <div id="box_no" class="row" style="display:none;"> <div class="col-md-2"> <img alt="Disappointed Smiley" src="../bilder/js-no.png" class="img-responsive script_icon"> </div> <div class="col-md-10"> <h3>What a shame!</h3> <p>We would have loved to show you.</p> <p>It's a very exciting and broad subject area with many possible applications..</p> <hr> <h3>Are you going to change your mind?</h3> <p> <button class="script_startover btn btn-default">Yes, start again</button> <button class="script_stop btn btn-primary">No, I really don't want to</button> </p> </div> </div> <div id="box_stop" style="display:none;"> <h3>Thank you for your participation in our survey.</h3> <div class="tucbox-tip-info"> <div class="script_emoji"> <p><strong>Feel free to give us feedback:</strong><br> How did you like the survey?</p> <div class="slider"> <input id="emoji_range" type="range" value="0" min="0" max="5" step="1"> </div> <div class="result"> <span class="emoji_icon">❓</span> <span class="emoji_text_en">no information</span> </div> </div> <p><button class="script_startover btn btn-default">OK</button></p> </div> </div> - JavaScript Code
$(document).ready(function() { // Ja/Nein-Befragung $("#script_yes").on("click", function() { $("#box_yes").show(); $("#box_start").hide(); }); $("#script_no").on("click", function() { $("#box_no").show(); $("#box_start").hide(); }); $(".script_startover").on("click", function() { $("#box_yes").hide(); $("#box_no").hide(); $("#box_start").show(); $("#box_stop").hide(); }); $(".script_stop").on("click", function() { $("#box_yes").hide(); $("#box_no").hide(); $("#box_start").hide(); $("#box_stop").show(); }); // Emoji-Slider const range = document.querySelector('#emoji_range'); const div_icon = document.querySelector('.script_emoji .emoji_icon'); const div_text_de = document.querySelector('.script_emoji .emoji_text_de'); const div_text_en = document.querySelector('.script_emoji .emoji_text_en'); const emojis = ['❓', '☹️', '😑', '😐', '🙂', '😄']; const emojis_text_de = ['keine Angabe', 'unzufrieden', 'weniger zufrieden', 'war okay', 'eher gut', 'sehr gut']; const emojis_text_en = ['no information', 'dissatisfied', 'less satisfied', 'was okay', 'rather good', 'very good']; range.addEventListener('input', (e) => { let rangeValue = e.target.value; div_icon.textContent = emojis[rangeValue]; if (div_text_de) { div_text_de.textContent = emojis_text_de[rangeValue]; } if (div_text_en) { div_text_en.textContent = emojis_text_en[rangeValue]; } }); });
Example of use: Checklist „Does a study program fit me?“
Interest and Motivation
| Completely true | Somewhat true | Somewhat untrue | Not true at all | |
|---|---|---|---|---|
| Am I interested in a particular field and eager to learn more about it? | ||||
| Can I stay engaged with a topic and work on it independently for an extended period of time? | ||||
| Am I willing to study regularly and persevere even when things get difficult? |
Learning and Working Style
| Completely true | Somewhat true | Somewhat untrue | Not true at all | |
|---|---|---|---|---|
| Can I organize my studies and learning independently? | ||||
| Do I enjoy reading and understanding longer texts or academic literature? | ||||
| Can I concentrate on a task even when it takes a long time to complete? |
Everyday Life and Future Plans
| Completely true | Somewhat true | Somewhat untrue | Not true at all | |
|---|---|---|---|---|
| Can I imagine investing several years in education and learning? | ||||
| Does studying fit with my career plans and goals? |
The evaluation occurs only within the current browser session and is not saved or shared with third parties.
Source Code for this Example
To implement such a checklist, HTML and JQuery are used:
- HTML Code
<h3 class="h4 linie">Interest and Motivation</h3> <table class="table table-bordered table-striped"> <colgroup> <col style="width: 60%;"> <col style="width: 10%;"> <col style="width: 10%;"> <col style="width: 10%;"> <col style="width: 10%;"> <tr> <th></th> <th>Completely true</th> <th>Somewhat true</th> <th>Somewhat untrue</th> <th>Not true at all</th> </tr> <tr class="optionen"> <td>Am I interested in a particular field and eager to learn more about it?</td> <td><label><input type="radio" name="frage1" id="frage1_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage1" id="frage1_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage1" id="frage1_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage1" id="frage1_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> <tr class="optionen"> <td>Can I stay engaged with a topic and work on it independently for an extended period of time?</td> <td><label><input type="radio" name="frage2" id="frage2_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage2" id="frage2_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage2" id="frage2_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage2" id="frage2_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> <tr class="optionen"> <td>Am I willing to study regularly and persevere even when things get difficult?</td> <td><label><input type="radio" name="frage3" id="frage3_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage3" id="frage3_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage3" id="frage3_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage3" id="frage3_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> </table> <h3 class="h4 linie">Learning and Working Style</h3> <table class="table table-bordered table-striped"> <colgroup> <col style="width: 60%;"> <col style="width: 10%;"> <col style="width: 10%;"> <col style="width: 10%;"> <col style="width: 10%;"> <tr> <th></th> <th>Completely true</th> <th>Somewhat true</th> <th>Somewhat untrue</th> <th>Not true at all</th> </tr> <tr class="optionen"> <td>Can I organize my studies and learning independently?</td> <td><label><input type="radio" name="frage4" id="frage4_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage4" id="frage4_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage4" id="frage4_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage4" id="frage4_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> <tr class="optionen"> <td>Do I enjoy reading and understanding longer texts or academic literature?</td> <td><label><input type="radio" name="frage5" id="frage5_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage5" id="frage5_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage5" id="frage5_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage5" id="frage5_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> <tr class="optionen"> <td>Can I concentrate on a task even when it takes a long time to complete?</td> <td><label><input type="radio" name="frage6" id="frage6_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage6" id="frage6_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage6" id="frage6_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage6" id="frage6_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> </table> <h3 class="h4 linie">Everyday Life and Future Plans</h3> <table class="table table-bordered table-striped"> <colgroup> <col style="width: 60%;"> <col style="width: 10%;"> <col style="width: 10%;"> <col style="width: 10%;"> <col style="width: 10%;"> <tr> <th></th> <th>Completely true</th> <th>Somewhat true</th> <th>Somewhat untrue</th> <th>Not true at all</th> </tr> <tr class="optionen"> <td>Can I imagine investing several years in education and learning?</td> <td><label><input type="radio" name="frage7" id="frage7_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage7" id="frage7_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage7" id="frage7_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage7" id="frage7_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> <tr class="optionen"> <td>Does studying fit with my career plans and goals?</td> <td><label><input type="radio" name="frage8" id="frage8_1" value="completely true"><span class="sr-only">Completely true</span></label></td> <td><label><input type="radio" name="frage8" id="frage8_2" value="somewhat true"><span class="sr-only">Somewhat true</span></label></td> <td><label><input type="radio" name="frage8" id="frage8_3" value="somewhat untrue"><span class="sr-only">Somewhat untrue</span></label></td> <td><label><input type="radio" name="frage8" id="frage8_4" value="not true at all"><span class="sr-only">Not true at all</span></label></td> </tr> </table> <p><button class="btn btn-default btn-selfcheck" disabled id="btnAuswertung">Show Evaluation</button></p> <p><em>The evaluation occurs only within the current browser session and is not saved or shared with third parties.</em></p> <div id="containerAuswertung" style="display: none;"> <hr> <h3>My Evaluation</h3> <div class="row"> <div class="col-xs-3 text-right"> <p>Completely true</p> </div> <div class="col-xs-8"> <div class="progressText" id="progressText1"></div> <div class="progress"> <div class="progress-bar progress-bar-success" id="progressBar1" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="19" style="width: 0%"> <span class="sr-only">40% Complete (success)</span> </div> </div> </div> </div> <div class="row"> <div class="col-xs-3 text-right"> <p>Somewhat true</p> </div> <div class="col-xs-8"> <div class="progressText" id="progressText2"></div> <div class="progress"> <div class="progress-bar progress-bar-info" id="progressBar2" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="19" style="width: 0%"> <span>20% Complete (info)</span> </div> </div> </div> </div> <div class="row"> <div class="col-xs-3 text-right"> <p>Somewhat untrue</p> </div> <div class="col-xs-8"> <div class="progressText" id="progressText3"></div> <div class="progress"> <div class="progress-bar progress-bar-warning" id="progressBar3" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="19" style="width: 0%"> <span class="sr-only">60% Complete (warning)</span> </div> </div> </div> </div> <div class="row"> <div class="col-xs-3 text-right"> <p>Not true at all</p> </div> <div class="col-xs-8"> <div class="progressText" id="progressText4"></div> <div class="progress"> <div class="progress-bar progress-bar-danger" id="progressBar4" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="19" style="width: 0%"> <span class="sr-only">80% Complete (danger)</span> </div> </div> </div> </div> <h4>Interpretation of your results</h4> <div class="tucbox-tip-info area-selfcheck" id="interpretation"> The evaluation shows the distribution of your answers across the different response options. It gives you an overview of how well your prerequisites for studying are met. </div> <p>The results of the self-check can be <a href="javascript:window.print()">printed or saved</a> and evaluated together with the study counseling.</p> </div> - JavaScript Code
$(document).ready(function() { $('input[type="radio"]').change(function() { // Activate evaluation button when all questions are answered var totalFragen = $('.optionen').length; var beantworteteFragen = $('input[type="radio"]:checked').length; if (beantworteteFragen === totalFragen) { $('#btnAuswertung').prop('disabled', false); } }); $('#btnAuswertung').click(function() { var auswertung = {}; // Check all questions var element = 'ol li'; if ($('table tr.optionen').length > 0) { var element = 'table tr.optionen'; } $(element).each(function(index) { var frageNummer = index + 1; var antwort = $(this).find('input[type="radio"]:checked').val(); if (antwort) { if (!auswertung[antwort]) { auswertung[antwort] = 0; } auswertung[antwort]++; } }); // Show evaluation container $('#containerAuswertung').show(); // Update progress bars var totalFragen = $('.optionen').length; var percent_value1 = Math.round((auswertung['completely true'] || 0) / totalFragen * 100); var percent_value2 = Math.round((auswertung['somewhat true'] || 0) / totalFragen * 100); var percent_value3 = Math.round((auswertung['somewhat untrue'] || 0) / totalFragen * 100); var percent_value4 = Math.round((auswertung['not true at all'] || 0) / totalFragen * 100); var sum_12 = (auswertung['completely true'] || 0) + (auswertung['somewhat true'] || 0); var sum_34 = (auswertung['somewhat untrue'] || 0) + (auswertung['not true at all'] || 0); $('#progressBar1').css('width', percent_value1 + '%').attr('aria-valuenow', percent_value1).text(percent_value1 + '%'); $('#progressText1').text(percent_value1 + '% (' + (auswertung['completely true'] || 0) + 'x answered)'); $('#progressBar2').css('width', percent_value2 + '%').attr('aria-valuenow', percent_value2).text(percent_value2 + '%'); $('#progressText2').text(percent_value2 + '% (' + (auswertung['somewhat true'] || 0) + 'x answered)'); $('#progressBar3').css('width', percent_value3 + '%').attr('aria-valuenow', percent_value3).text(percent_value3 + '%'); $('#progressText3').text(percent_value3 + '% (' + (auswertung['somewhat untrue'] || 0) + 'x answered)'); $('#progressBar4').css('width', percent_value4 + '%').attr('aria-valuenow', percent_value4).text(percent_value4 + '%'); $('#progressText4').text(percent_value4 + '% (' + (auswertung['not true at all'] || 0) + 'x answered)'); // Update interpretation text var interpretationText = ''; if ((percent_value1 + percent_value2) > 60) { interpretationText = 'Your answers suggest <strong>very good prospects</strong> for a study program.'; } else if ((percent_value3 + percent_value4) > 60) { interpretationText = 'Your answers suggest that a study program <strong>might not be the right choice</strong> at this time.'; } else { interpretationText = 'Your answers show a balanced distribution. This indicates that a study program <strong>could be suitable</strong>. It is recommended to consider time management and motivation.'; } $('#interpretation').html(interpretationText).addClass('tucbox-tip').removeClass('tucbox-tip-info'); }); });