Assist with JavaScript Programming

Can someone assist me in creating functionality for the "Show Picture" button to toggle the visibility of the picture when clicked? I've been trying to achieve this using an If/Else statement for a school project but have been struggling with it. Any help would be appreciated!

$(document).ready(function() {

  $('#showA').on('click', function() {
    $('#house1').css('opacity', 1).fadeIn('slow');
  });

  // Additional code for other houses removed for brevity

});
img {
  padding: 29px;
  opacity: 0;
  width: 250px;
  height: 250px;
}

// Additional CSS styling removed for brevity

}
<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <link rel="stylesheet" type="text/css" href="css/finalproject.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script type="text/javascript" src="js/finalproject.js">
  </script>
  <title>Final Project</title>
</head>

<body>
  <center>
    <h1>Bazegian Cape Cod Rental Homes</h1>
  </center>

  <div>
    <form id="chatham">
      <label><b><u>Chatham, MA</u></b></label><br>
      // Form elements for Chatham house removed for brevity
    </form>
  </div>

  // Additional form elements for other houses removed for brevity

  <img id="house1" src="images/chatham.jpg" />
  <img id="house2" src="images/wellfleet.jpg" />
  <img id="house3" src="images/dennis.jpg" />
  <img id="house4" src="images/provincetown.jpg" />
</body>

</html>

Answer №1

Initiate the opacity to 0 initially for all images, then display the correct image

$('img').css('opacity',0);

$('.show').on('click', function() {
  $('img').css('opacity', 0);
  var ind = $(this).closest('.house').parent().index();
  $('img').eq(ind).css('opacity', 1).fadeIn('slow');
});

$('.reset').click(function() {
  $(this).parent('form').find('input[type="text"]').val('');
});

$('.submit').click(function() {
  window.alert("You will be contacted through email shortly in order to process your payment method for the home in Chatham, MA. Thank you for choosing Bazegian Cape Cod Rental Homes!");
});
img {
  padding: 29px;
  opacity: 0;
  width: 250px;
  height: 250px;
}

div {
  vertical-align: top;
  width: 267px;
  height: auto;
  display: inline-block;
  padding: 20px;
  margin: 0px;
  border-radius: 10px;
  border: 1px solid;
}

p {
  margin: 5px;
  padding: 3px;
  background-color: blue;
  border-radius: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center>
  <h1>Bazegian Cape Cod Rental Homes</h1>
</center>

<div>
  <form class="house">
    <label><b><u>Chatham, MA</u></b></label><br>
    <label><b>$300 a Day</b></label><br>
    <label>Name:</label><br>
    <input name="nameA" type="text" /><br>
    <label>Email Address:</label><br>
    <input name="emailA" type="text" /><br>
    <label>Phone Number:</label><br>
    <input name="phoneA" type="text" /><br>
    <label>How many days would you like to rent?</label><br>
    <select>
  <option value="5-10">5-10 Days</option>
  <option value="6-7">10-15 Days</option>
  <option value="8-9">15-20 Days</option>
</select>
    <input class="show" type="button" value="Show Picture">
    <input class="submit" type="button" value="Submit">
    <input class="reset" type="button" value="Reset">
  </form>
</div>

<div>
  <form class="house">
    <label><b><u>Wellfleet, MA</u></b></label><br>
    <label><b>$250 a Day</b></label><br>
    <label>Name:</label><br>
    <input name="nameB" type="text" /><br>
    <label>Email Address:</label><br>
    <input name="emailB" type="text" /><br>
    <label>Phone Number:</label><br>
    <input name="phoneB" type="text" /><br>
    <label>How many days would you like to rent?</label><br>
    <select>
  <option value="5-10">5-10 Days</option>
  <option value="6-7">10-15 Days</option>
  <option value="8-9">15-20 Days</option>
</select>
    <input class="show" type="button" value="Show Picture">
    <input class="submit" type="button" value="Submit">
    <input class="reset" type="button" value="Reset">
  </form>
</div>

<div>
  <form class="house">
    <label><b><u>Dennis, MA</u></b></label><br>
    <label><b>$350 a Day</b></label><br>
    <label>Name:</label><br>
    <input name="nameC" type="text" /><br>
    <label>Email Address:</label><br>
    <input name="emailC" type="text" /><br>
    <label>Phone Number:</label><br>
    <input name="phoneC" type="text" /><br>
    <label>How many days would you like to rent?</label><br>
    <select>
  <option value="5-10">5-10 Days</option>
  <option value="6-7">10-15 Days</option>
  <option value="8-9">15-20 Days</option>
</select>
    <input class="show" type="button" value="Show Picture">
    <input class="submit" type="button" value="Submit">
    <input class="reset" type="button" value="Reset">
  </form>
</div>

<div>
  <form class="provincetown">
    <label><b><u>Provincetown, MA</u></b></label><br>
    <label><b>$300 a Day</b></label><br>
    <label>Name:</label><br>
    <input name="nameD" type="text" /><br>
    <label>Email Address:</label><br>
    <input name="emailD" type="text" /><br>
    <label>Phone Number:</label><br>
    <input name="phoneD" type="text" /><br>
    <label>How many days would you like to rent?</label><br>
    <select>
  <option value="5-10">5-10 Days</option>
  <option value="6-7">10-15 Days</option>
  <option value="8-9">15-20 Days</option>
</select>
    <input class="show" type="button" value="Show Picture">
    <input class="submit" type="button" value="Submit">
    <input class="reset" type="button" value="Reset">
  </form>
</div>

<img class="img" src="images/chatham.jpg" />
<img class="img" src="images/wellfleet.jpg" />
<img class="img" src="images/dennis.jpg" />
<img class="img" src="images/provincetown.jpg" />

ps: it is highly recommended to refactor and optimize your codebase

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 rotating individual letters within a sentence on popular web browsers, including Chrome

Can you make a sentence appear with each individual letter rotating and getting larger? This is my approach: I decided to split the sentence into separate span elements, one for each letter. Then I used CSS transform -webkit-transform to animate the lette ...

Unable to refresh the view from the controller once the promise has been resolved

On my webpage, I want to display a dynamic list of items that updates whenever the page is refreshed. To achieve this, I am using Parse to store and retrieve my items using promises. Here's a simplified example of how it works: When the index.html pa ...

Create an HTML document that includes data sent via AJAX in the callback function

Currently, I am developing a module that triggers an AJAX request when the user clicks on the download button. Once the call is completed, some data is returned and I need to write this data into an HTML file and automatically save it on the client side. ...

Mastering Typescript lookup types - effectively limit the properties included in a merge operation with the Partial type

Exploring lookup types, I'm interested in creating a safe-merge utility function that can update an entity of type T with a subset of keys from another object. The objective is to leverage the TypeScript compiler to catch any misspelled properties or ...

Does AngularJS have a feature similar to jQuery.active?

As I utilize selenium to conduct tests on my application, I am encountering numerous ajax calls that utilize $resource or $http. It would be convenient if there was a method in angular to monitor active ajax requests so that selenium could wait until they ...

Tips for showing a message in the modal after a user completes the registration process

This code snippet contains an HTML form for user registration. Upon clicking the register button, users are redirected to another page. However, the desired outcome is to display the message on the same modal form. <div class="popup"> ...

What is the best way to fill a dropdown menu with names and corresponding numeric IDs?

How can I effectively link my dropdown to a variable in the controller, using the id property of the dropdown array instead of the value for assignment? Meanwhile, still displaying the content of the drop down using the name property of the array for user ...

Invalidating the memory in iOS 7.1.1 when using canvas drawImage

When I use the following code on an animation frame, I notice a significant memory leak that eventually causes IOS Safari or Chrome to crash: ctx.drawImage(anotherCanvas, clipX, clipY, clipW, clipH, x, y, w, h); Interestingly, if I don't apply a ...

Exploring the TLS configuration for a Node.js FTP server project: ftp-srv package

I'm seeking to comprehend the accurate setup of TLS for my FTP project (typescript, nodejs) using this API: ftp-srv The documentation provided is quite basic. In one of the related github issues of the project, the author references his source code / ...

Numerous XMLHttp requests causing JSON data to be overwritten

I am facing an issue where my JSON data is being overwritten by the result of the last XMLHttpRequest made for each country. Is there a way to prevent this from happening? Below is the code snippet in question: function getDataBetween() { for (var i = ...

Is it possible to show an image without altering the Box dimensions?

Hi there, I am currently working on designing a footer and I have encountered an issue. I want to add an image in a specific position, but when I do so, it affects the size of the box. I was wondering if there is a way to set the image as a background or b ...

Struggling to Load: Ajax Request to Google App Engine Causing Page to

I have developed a page that communicates with a Python application running on Google App Engine to retrieve JSON data using JSONP for cross-origin functionality. However, I am encountering an issue where the page hangs and fails to display the data no mat ...

Setting up the Firebase emulator: should you use getFirestore() or getFirestore(firebaseApp)?

After delving into the process of connecting your app to the Firebase emulators like Firestore emulator, I came across the primary documentation which outlined the steps for Web version 9: import { getFirestore, connectFirestoreEmulator } from "fireba ...

Make the textarea larger and bring it to the forefront when it is selected

I would like to make a textarea expand (increase its height) when it is in focus. The expanded textarea should not push the content down, but rather be displayed above other content. Currently, this is the code I am using (check out the example here): $( ...

Exploring the contrast between 'completed' and 'upcoming' in callback functions within node.js

Within the passport documentation for authentication configuration, there is a function that appears rather intimidating as it utilizes the enigmatic function "done." passport.use(new LocalStrategy( function(username, password, done) { User.findOne( ...

What are the reasons behind Jquery's decreased speed performance in Internet Explorer?

My ASP.NET page is quite intricate and heavily depends on jQuery for manipulating the DOM without using AJAX. Interestingly, I've observed that the page performs better in Mozilla browsers like Firefox compared to Internet Explorer 7 or 8. Could it b ...

Learning to Use jQuery to Send JSON Requests in Rails

I am attempting to send a JSON post request to a Rails 3 server. Here is the AJAX request I have set up: $.ajax({ type: 'POST',<br> contentType: "application/json",<br> url: url, ...

Importing a JavaScript file into another JavaScript file as text in React Native can be a convenient way

In my project, I have a file named MyFirstPage.js that contains the following code snippet: renderCards() { let icon = this.state.icon; .... .... .... } This code is responsible for rendering cards within the main render function. However, as the ...

Eliminate Bootstrap's validation glyphs

Issue Description I am attempting to eliminate the validation icons from Bootstrap, such as the "x" and "check" symbols. Despite my efforts to search thoroughly, I have been unable to locate where these icons are located in the code. Code Sample You can ...

Resolve the issue of text overlapping on an image when zooming in

There seems to be an issue with overlapping text and images when zooming the page. I have included a screenshot for reference, please take a look and provide a solution. Thank you in advance.https://i.sstatic.net/oVlGN.png Here is the CSS code causing the ...