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

GWT's Stylish Image Carousel

Can anyone recommend a simple image slider for GWT that meets the following requirements: Slides several images from right to left when user clicks on the image (selected image should be at the center) Has endless looping capability (the last image shoul ...

Automate your Excel tasks with Office Scripts: Calculate the total of values in a column depending on the criteria in another column

As a newcomer to TypeScript, I have set a goal for today - to calculate the total sum of cell values in one column of an Excel file based on values from another column. In my Excel spreadsheet, the calendar weeks are listed in column U and their correspon ...

Elements vanish when SHAKE effect is in use

I've been experimenting with this framework and I'm struggling to get the shaking effect to work properly. Whenever I hover over an element, other divs seem to disappear. I tried using different versions of JQuery and JQuery UI on JSFiddle, and i ...

Different ways to showcase a value from the CSS file on the console using console.log

In this guide, you can learn how to create a custom directive in Angular by following this tutorial: Custom Directive Tutorial. The directive should function as intended. Still, I want to see the color value set in the CSS file displayed on the console us ...

The VueJS app seems to be experiencing difficulties with rendering the content

Just starting out with VueJS and I have my initial files - index.html and index.js. I want to stick with just these two files and not add any more. Here's the content of index.html: <html lang="en"> <head> <meta charset ...

NPM encountered difficulties in resolving the dependency tree

I seem to be encountering a persistent issue that I cannot resolve on my own. My attempt to update webpack and webpack-cli in a project has been met with repeated errors of this nature: npm install webpack@latest --save-dev npm ERR! code ERESOLVE npm E ...

AngularJS - sorting JSON data based on key values

I am working with a JSON data set that I need to filter based on the selected option value. The select input is bound to an ng-model, but for some reason, the filter isn't functioning properly. Can anyone spot what mistake I might be making? This is ...

Understanding the time complexity of Object.entries()

Is the complexity of Object.entries() in JavaScript known? According to information from this question, it seems like it could possibly be O(n) if implemented by collecting keys and values as arrays and then combining them together? ...

List style image does not serve its intended purpose

I attempted to personalize my webpage by adding an image to a list, but I couldn't get the list-style image to work. You can view the page at Below is the code I used: CSS /* Fleet List */ ul#flotta li { list-style-image:url("http://ctp.serviziew ...

Is it feasible to place an image on top of a box?

Hello everyone, I have been trying to figure out how to achieve a specific layout in React. I am using MUI so there is no need for any hard-coded CSS. However, my attempts have not been successful so far. The layout I am aiming for consists of an image on ...

Utilizing the Selenium driver to test the functionality of SpreadJS

Is there a way to trigger click events based on cell position in spreadJs? I attempted the following: sheet.setActiveCell(3,3); sheet.cellClick(); as well as sheet.getCell(3,3).cellClick(); However, I have not been successful. Any help or suggestions ...

How can I dynamically set the width of a span element in HTML?

Hello there! As a beginner in html and angularjs, I'm experimenting with dynamically assigning span width based on an angular js response. <div class="statarea" ng-repeat="x in names"> <div class="ratingarea"> <div class=" ...

How to style TextInput to display dollar amounts when using onChangeText and Redux in React Native

Struggling to format number input in a TextInput using the onChangeText function in React Native with Redux. Tried using .toFixed(2) function, but encountering an issue where it interprets the next digit as the first decimal and rounds back to 0 due to Re ...

I'm perplexed as to why my JavaScript code isn't successfully adding data to my database

Recently, I delved into the world of NodeJS and Express. My goal was to utilize Node and Express along with MongoDB to establish a server and APIs for adding data to the database. Specifically, I aimed to write the code in ESmodules syntax for JavaScript. ...

Creating a unique tab spacing effect using HTML and CSS

I'm currently working on my personal website and I'm looking to create a tab-like effect (similar to word processors) for showcasing projects along with their descriptions. Here's an example layout: ReallyLong NameProject Project1 Descr ...

Triggering a specific outcome with every user interaction involving the selection of an element

How can I trigger this effect each time the user clicks on the element using jQuery? I have added a "ripple" class upon clicking, but when I click on the element for the second time, the effect does not execute because the class has already been added. Ho ...

Efficiently organizing dates in the Vuetify date range picker

I am working with a vuetify date range picker component and have the following code: <v-menu ref="effectiveDateMenu" v-model="effectiveDateMenu" :close-on-content-cl ...

Feeling trapped during the process of setting up a intricate jQuery Image Slider

I've hit a roadblock while trying to make changes to the slider located at THIS URL. In the current setup, each thumbnail corresponds to one main display image. Clicking on a thumbnail displays a single image and then proceeds to slide to the next th ...

Is there a way to adjust the label elevation for a Material UI TextField component?

I am trying to enhance the appearance of a textfield label, but I am facing some challenges with my code. textStyle: { backgroundColor: '#1c1a1a', border: 0, borderRadius: 0, width: 400, height: 66, display: 'flex&a ...

The deletion request using the form in Express is experiencing issues and not functioning properly

When attempting to delete data from a database using a form in node.js and express, I am encountering issues with the deletion process. It seems that there are only two methods available - get and post - and no specific delete method. router.js code rout ...