SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit ef4bb178 authored by Maurits Moeys's avatar Maurits Moeys
Browse files

refactored jQuery switch

parent 71108359
No related branches found
No related tags found
No related merge requests found
...@@ -4,61 +4,44 @@ ...@@ -4,61 +4,44 @@
{% block bodysup %} {% block bodysup %}
<script> <script>
$(document).ready(function(){ $(document).ready(function(){
$('label[for=id_pub_title]').hide()
$('input#id_pub_title').hide() // Looking at the DOM we can see that all the relevant stuff we want to hide/show is packed in a table. Hint: to play around with jquery, go to devtools, and just write your selectors there. $('tbody') will grab the table from the DOM I just mentioned.
$('label[for=id_author_list]').hide()
$('input#id_author_list').hide() // Hiding all the rows for initial set-up
$('label[for=id_pub_date]').hide() var allToggableRows = $('tr').slice(1) // Slices off the first element of the array. Read the documentation on this one, its quite useful. Same for .each and .map in upcoming code.
$('input#id_pub_date').hide()
$('label[for=id_arxiv_link]').hide() allToggableRows.each(function(index){
$('input#id_arxiv_link').hide() $(this).hide() // this is the object we're handling in the current iteration of the loop. It is wrapped in $(' ... ') to make it an jQuery object, so that we can call a jQuery function on it like .hide()
$('label[for=id_pub_DOI_link]').hide() })
$('input#id_pub_DOI_link').hide()
$('label[for=id_pub_abstract]').hide() // Indices to remove in order to show thing named by variable name
$('textarea#id_pub_abstract').hide() var preprint = [2,4]
var published = [3]
function show(indices){
allToggableRows.each(function(index){
if ($.inArray( index, indices) != -1){
$(this).hide()
}else{
$(this).show()
}
})
}
$('select#id_type').on('change', function() { $('select#id_type').on('change', function() {
var selection = $(this).val(); var selection = $(this).val();
$('label[for=id_pub_title]').show()
$('input#id_pub_title').show()
$('label[for=id_author_list]').show()
$('input#id_author_list').show()
switch(selection){ switch(selection){
case "published": case "published":
$('label[for=id_pub_date]').show() show(published)
$('input#id_pub_date').show()
$('label[for=id_arxiv_link]').hide()
$('input#id_arxiv_link').hide()
$('label[for=id_pub_DOI_link]').show()
$('input#id_pub_DOI_link').show()
$('label[for=id_pub_abstract]').show()
$('textarea#id_pub_abstract').show()
break; break;
case "preprint": case "preprint":
$('label[for=id_pub_date]').hide() show(preprint)
$('input#id_pub_date').hide()
$('label[for=id_arxiv_link]').show()
$('input#id_arxiv_link').show()
$('label[for=id_pub_DOI_link]').hide()
$('input#id_pub_DOI_link').hide()
$('label[for=id_pub_abstract]').show()
$('textarea#id_pub_abstract').show()
break; break;
default: default:
$('label[for=id_pub_title]').hide() allToggableRows.hide()
$('input#id_pub_title').hide()
$('label[for=id_author_list]').hide()
$('input#id_author_list').hide()
$('label[for=id_pub_date]').hide()
$('input#id_pub_date').hide()
$('label[for=id_arxiv_link]').hide()
$('input#id_arxiv_link').hide()
$('label[for=id_pub_DOI_link]').hide()
$('input#id_pub_DOI_link').hide()
$('label[for=id_pub_abstract]').hide()
$('textarea#id_pub_abstract').hide()
} }
}); });
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment