2

I am trying to make a simple Nodejs/Express app that will show an image of a beach when the SHOW button is clicked and then show an image of a lake when the SHOW button is clicked again.

It works as a simple js app but I am trying out Nodejs and Express for the first time.

The beach and lake photos are stored in the images folder of my app. Here is my code

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;

index.pug

extends layout

block content


  h1= title
  h4= "Click button below to see a photo of a beach"

  <form id="show">
    <button type="submit" class="btn btn-primary btn-margin">Show</button>
  </form>

  <div id="img">

  </div>

  <div class="image1">
    <img src="img/beach-pic.jpg" alt="photo of beach" id="img1">
    <img src="img/lake-pic.jpg" alt="photo of lake" id="img2">
  </div>

javascript/jquery

$(document).ready(function(){
  $("form#show").submit(function(){
    event.preventDefault();

    $("#img1").toggle();
    if($("#img1").css('display') == 'inline'){
      $("#img2").hide();
    } else {
      $("#img2").show();
    }
  });
});

css

body {
  padding: 50px;
  font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
  color: #00B7FF;
}

#img1 {
  display: none;
}

#img2 {
  display: none;
  height: 500px;
  width: 650px;
}
3
  • No Image -> On Click "Show" -> Image One -> On Click "Show" -> Image Two -> On Click "Show" -> Image One and so on...is this what you're looking for? Commented Jul 22, 2018 at 4:05
  • 1
    @ Dhaval Jardosh: Yes it is! Commented Jul 22, 2018 at 4:24
  • Answer added, please let me know if that's what you're looking for. Commented Jul 22, 2018 at 4:36

3 Answers 3

1

Use display:none for both images and toggle when required.

Only thing is to handle the show of your 1st image, that we have done by 1st if-else statement.

$(document).ready(function() {
  $("#show").submit(function(event) {
    event.preventDefault();

    //Handle 1st Image Render
    if ($("#img1").css('display') == 'none' && $("#img2").css('display') == 'none') {
      $("#img1").css('display', 'block');
    }


    //Toggle After 1st render
    else if ($("#img1").css('display') == 'block') {
      $("#img2").css('display', 'block');
      $("#img1").css('display', 'none');
    } else if ($("#img2").css('display') == 'block') {
      $("#img1").css('display', 'block');
      $("#img2").css('display', 'none');
    }


  });
});
#img1,
#img2 {
  display: none;
  width: 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="show">
  <button id="lol" class="btn btn-primary btn-margin">Show</button>
</form>
<div id="img">

</div>

<div class="image1">
  <img src="https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=350" alt="photo of beach" id="img1">
  <img src="https://images.pexels.com/photos/247600/pexels-photo-247600.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=350" alt="photo of lake" id="img2">
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

I made the required changes but its still not working. Here is the link for where the app is being hosted on Azure: myexpressapp-angryfaiza.azurewebsites.net
Please check the images, are the routes to the image correct. myexpressapp-angryfaiza.azurewebsites.net/… - Let me know what you're able to see here
I am now using the same image links that were in your example, so they should work. The app is working fine on the localhost, just not in azure
so check your db for images.
0

Everything looks good, what exactly is it doing right now? Are your images showing up on the page? Are your Node endpoints working properly?

2 Comments

No images are showing up on the page at all. The button is not working at all.
I think its because my button is not connecting to the jquery
0

I don't know what is present in your layout, but i think one possible error is that you might not have included jQuery or the JavaScript file in your pug file, because i tried to run a very similar code and it ran perfectly alright for me. Another possible error is the order of including the two, make sure to include jQuery first and then your js file. If it still doesn't work then check your console for any potential errors, it usually helps.

1 Comment

Can you tell me how to include the JQuery and the JavaScript file in the index.pug?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.