Showcase your skills in CSS, HTML, and Javascript

I attempted to search for something similar but came up empty-handed.

I have two buttons. When I click one of them, I want to hide a certain division, but it's not working as expected. The divs d2 and d3 are initially hidden when the page opens, which is fine. However, when I try to click on one of the buttons, nothing happens. Is there another way to achieve what I'm trying to do? Am I making a mistake somewhere?

<head><title>Add Education and Work Experience</title>
<script type='text/javascript'>

 function Funz1()
    {
        document.getElementById("d1").style.display = "none";
        document.getElementById("d2").style.display = "none";
        document.getElementById("d3").style.display = "block";

    }


function Funz2()
    {
        document.getElementById("d1").style.display = "none";
        document.getElementById("d2").style.display = "block";
        document.getElementById("d3").style.display = "none";

    }

function checkForm3()
 {
 // Checking that all fields in the registration are filled
    if(document.registration_form.luogo.value == '' ||
   document.registration_form.titolo.value == '' || 
   document.registration_form.anno.value == ''
   )
{
    alert('Please fill in all required fields');
    return false;
}
// If we reach here, everything is okay
return true;
}

function checkForm4()
{
// Checking that all fields in the registration are filled
if(document.registration_form.luogo2.value == '' ||
   document.registration_form.ruolo.value == '' || 
   document.registration_form.anno2.value == ''
   )
{
    alert('Please fill in all required fields');
    return false;
}
// If we reach here, everything is okay
return true;
}
</script>

<meta charset="utf-8"> 
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" 

href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 <script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
 <script 
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> 
 </script>
 </head>
<body>

<br>
<div id="d1">

<button type="button" onclick="Funz1()" >Add Work Experience</button>              
 <button type="button" onclick="Funz2()" > Add Study Experience</button> 

</div>



<div id="d2" style= display:none>
<br><form name='studio_form' action='Esperienze.php' method='post' 
 onsubmit='return checkForm3()' >
        <div class='row'>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='titolo'>Title</label>
<input type='text' name='titolo'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='year'>Year</label>
<input type='text' name='anno'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='location'>Location</label>
<input type='date' name='luogo'>
</div>



<div class='form-group col-xs-12 col-md-12 col-sm-12 col-lg-12'>                
<input name='submit' type='submit' value='Add'>     
</div>      

</div>                
</form>
</div>








<div id="d3" style= display:none>
<form name='work_form' action='Esperienze.php' method='post' 
onsubmit='return 
checkForm4()' disabled>
        <div class='row'>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='role'>Role</label>
<input type='text' name='ruolo'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='year2'>Year</label>
<input type='text' name='anno2'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='location2'>Location</label>
<input type='date' name='luogo2'>
</div>



<div class='form-group col-xs-12 col-md-12 col-sm-12 col-lg-12'>                
<input name='submit' type='submit' value='Add'>     
</div>      

</div>                
</form>
</div>

 </body>   



</html>

Answer №1

Completing the if() statement with || will expect another condition instead of a closing tag.

<head><title>Add Studies and Jobs</title>
<script type='text/javascript'>

 function Function1()
    {
        document.getElementById("d1").style.display = "none";
        document.getElementById("d2").style.display = "none";
        document.getElementById("d3").style.display = "block";

    }


function Function2()
    {
        document.getElementById("d1").style.display = "none";
        document.getElementById("d2").style.display = "block";
        document.getElementById("d3").style.display = "none";

    }

function checkForm5()
 {
 // Check that all fields in the registration are filled
    if(document.registration_form.location.value == '' ||
   document.registration_form.title.value == '' || 
   document.registration_form.year.value == ''
   )
{
    alert('Please enter all data marked with an asterisk');
    return false;
}
// If we get here, everything is okay
return true;
}

function checkForm6()
{
// Check that all fields in the registration are filled
if(document.registration_form.location2.value == '' ||
   document.registration_form.role.value == '' || 
   document.registration_form.year2.value == ''
   )
{
    alert('Please enter all data marked with an asterisk');
    return false;
}
// If we get here, everything is okay
return true;
}
</script>

<meta charset="utf-8"> 
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" 

href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 <script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
 <script 
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> 
 </script>
 </head>
<body>

<br>
<div id="d1">

<button type="button" onclick="Function1()" >Add Work Experience</button>              
 <button type="button" onclick="Function2()" > Add Study Experience</button> 

</div>



<div id="d2" style= display:none>
<br><form name='study_form' action='Experiences.php' method='post' 
 onsubmit='return checkForm5()' >
        <div class='row'>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='title'>Title</label>
<input type='text' name='title'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='year'>Year</label>
<input type='text' name='year'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='location'>Location</label>
<input type='date' name='location'>
</div>



<div class='form-group col-xs-12 col-md-12 col-sm-12 col-lg-12'>                
<input name='submit' type='submit' value='Add'>     
</div>      

</div>                
</form>
</div>



<div id="d3" style= display:none>
<form name='job_form' action='Experiences.php' method='post' 
onsubmit='return 
checkForm6()' disabled>
        <div class='row'>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='role'>Role</label>
<input type='text' name='role'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='year2'>Year</label>
<input type='text' name='year2'>
</div>

<div class='form-group col-xs-12 col-md-6 col-sm-6 col-lg-6'>            
<label for='location2'>Location</label>
<input type='date' name='location2'>
</div>



<div class='form-group col-xs-12 col-md-12 col-sm-12 col-lg-12'>                
<input name='submit' type='submit' value='Add'>     
</div>      

</div>                
</form>
</div>

 </body>

PS: Have you tried using the browser's developer tools, as it may show you an error message like

Uncaught SyntaxError: Unexpected token )
pointing to the line where the error occurred.

Answer №2

Not sure if this could be triggering the issue, but there seems to be an opening script tag in your code snippet:

<script 

It is located right after the link to your CSS stylesheet:

<link rel="stylesheet" 

href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for triggering a postback with jquery autocomplete when an option is selected

TEST AREA I've put together a basic demo of jquery-ui autocomplete. QUERY How do I trigger a postback when a selection is made from the autocomplete list? HTML <select id="ddl"></select> <input id="field" type="text"></input& ...

What can cause my function to return true on the server but false on the client side in Meteor.js?

I am facing an issue with the hasCompleted function which returns true or false on the server side: Smaples.helpers({ hasCompleted(userId) { // … switch (this.frequency) { case Frequency.ONE_TIME:{ return measures.fetch().every(mea ...

Challenges with Access-Control-Allow-Origin within the website's domain

Why am I encountering an issue when attempting to make an XMLHTTPRequest from a JavaScript file to a web service on the same domain, resulting in: Access-Control-Allow-Origin error stating that the origin is not allowed? Switching mydomain.com to localh ...

Caller Function's Parents in JavaScript

I have functions in JavaScript that need to verify if they are being called by the correct function (for example, it requires the function something.stuff.morestuff.coolFunction to initiate it). I attempted using Function.caller, but it only gives me the f ...

Utilizing mutation observers for monitoring dynamic changes in fetch request outcomes

I'm currently developing a specialized interface that showcases the cumulative ticket count (an integer representing the total) sourced from a 3rd party API. My goal is to trigger a notification whenever this count increases. I've come across inf ...

The CSS dropdown menu is malfunctioning on Internet Explorer and Firefox

After searching for a solution and coming up empty-handed, I have decided to ask a question. The drop-down menu on my computer and other devices at home works perfectly in Chrome but not in Firefox and IE. #menu { position: absolute; left: 50%; bottom ...

How can we convert a mixed-case word into one or more words with initial capital letters?

Is there a way to convert caps and spaces to their normal form? For instance, can the word coreControllerC4a be transformed into Core Controller C4a automatically when a function is triggered? ...

The function is coming back with a value of undefined

I need some assistance with a function I have below. This function is meant to download files one by one and move them to a directory called templates. At the end, it should return the length of the directory. However, it seems to be returning an undefined ...

Set a variable to represent a color for the background styling in CSS

My goal is to create an application that allows users to change the background color of a button and then copy the CSS code with the new background color from the <style> tags. To achieve this, I am utilizing a color picker tool from . I believe I ...

Why isn't my page width adjusting to various screen resolutions?

Hello everyone, I'm currently working on a website using two monitors - one with 1920x1080 resolution and the other with 1440x900 resolution. However, the website doesn't seem to fit properly on the 1440x900 screen. I've used percentage widt ...

typescript scrolling location

In my Angular UI code, I have a component class that includes the following structure: app.component.html //... <div class="banner"> <p-dialog [(visible)]="displayCOI" styleClass="coiDialog" [contentStyle]=" ...

Transforming the jQuery tooltip to be shown in a column layout

Hello, I am currently using the displayTag library to showcase some tables. My goal is to include a tooltip on each display:column element by utilizing jQuery. Below is the code snippet in question: <c:set var="titleName"><wp:i18n key="FILENAME" ...

Guide on implementing register helpers with node.js and express handlebars

When loading a record, I have a select option on my form and want to pre-select the saved option. Here is the code: The Student.hbs file displays the form, with the act obj coming from the student.js route student.hbs <form> <div class="for ...

Is it possible to trigger a function using an event on "Any specified selector within a provided array"?

Trying to figure out how to show/hide a group of divs with different IDs by executing a function after creating an array of IDs for the NavBar. I'm having trouble getting started, but here's what I was thinking: $.each(array1, function(i, value) ...

Implementing a click event on a selection option – tips and tricks

When I want to select an option, I can use the following script: Javascript $("#practice-area optgroup option").click(function(){ // insert your function here }); HTML <select name="practice-area" id="practice-area"> <option value="0">S ...

What is the proper way to utilize the src attribute for connecting HTML code?

I've noticed that in HTML you can include scripts like this: <script src="myscripts.js"></script> But I was wondering, is it possible to link an entire HTML page? Something like this: <html src="testing.html">< ...

JavaScript Tutorial: Extracting the text content of a drop event

Curious about extracting the text value from a drop event, I set out to find an answer. To my surprise, I discovered that it's not as straightforward as I thought. Check out the demo below: <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

Assist me in minimizing redundant code in a basic jQuery function

I successfully created a carousel using the jQuery cycle plugin with 4 links that correspond to different slides. Currently, I have separate blocks of code for each link, but I'm looking to optimize by creating a single function. $('#features-sl ...

Replace or update the current array of objects that align with the incoming array

I've been struggling to figure out how to find and replace an array that matches the existing one. The issue lies with using the some method. Here is the current array: let existingData = [{ id: 1, product: 'Soap', price: ' ...

Is it possible to extract content from a remote website by utilizing javascript and iframe?

Are there any resources available for working with iframes in Ruby on Rails? **UPDATE:** I have a link that directs to an external website. I am looking to extract the content from that site and save it within my application. Is this achievable without re ...