Change the image displayed in a div based on certain conditions dynamically

I am currently developing a website that is solely built using Django. I am interested in having either of two images displayed under a certain condition, similar to the example shown in this VueJs demonstration.

Is it achievable without utilizing a frontend framework? If so, what would be the most effective method to accomplish this task?

Answer №1

One way to achieve this in Vanilla Javascript is as follows:

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <img src="img1.jpg" id="image" alt="">
  </body>
  <script type="text/javascript">
    var img = document.getElementById('image');
    if (condition){
      img.src = "img2.jpg";
    }
  </script>
</html>

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

Unsupported MIME format

I'm encountering an issue where my stylesheet is not applying to my handlebars. It was working fine yesterday, but today I'm seeing a MIME error and I'm unsure of the reason behind it. Any assistance would be greatly appreciated. Server Cod ...

Send a React component to another component as a prop

I am looking to pass the entire parent component as props to its child component. However, when attempting to pass data from parent to child, I ended up importing the child into the parent. Is there another way to achieve this? Any suggestions on how I c ...

dismiss facial recognition event

Does anyone know how to implement a cancel button in Facebox when using it to delete data from MySQL? I want the window to unload when the user clicks on cancel. This is the code snippet I am currently using with Facebox: Are You Sure You Want To Delete ...

Create a modal dialog in CSS that dynamically adjusts its size without extending beyond the edges of the screen

I'm in the midst of developing a web application specifically for tablets. My goal is to display a modal dialog with a title bar and body that dynamically adjusts in height based on its content. If the content exceeds the viewport size, I want the dia ...

Tips for positioning items inside a container

I'm struggling to align a button with text inside of a well. Here is what I currently have: <div class="container"> <div class="row"> <div class="col-lg-12"> <div class="well"><h4>Download our 2 ...

Inject a dynamic URL parameter into an iframe without the need for server-side scripting

I'm really stuck and could use some assistance with the following issue, as I am unable to solve it on my own :( When a user is redirected to a form (provided via an iframe), there is a dynamic URL involved: website.com/form?id=123 The code resp ...

When the mouse is clicked, rotate the object along its axis using OBJ Loader in THREE.js

I am looking to add a feature where my object rotates on its axis when the mouse is dragged. The challenge I am facing is that I can only access my skull object within the function, which limits where I can place a rotation increment inside render(). Coul ...

Ways to expand the width of a div on hover - Angular navbar dropdown utilizing Bootstrap 4

I am attempting to make the div full width while also implementing a slideDown/Up effect. I am struggling to identify the specific CSS needed to adjust the width accordingly. Currently, this is what I have: I can see that the class open is dynamically ad ...

Access file using operating system's pre-installed application

How can I open a file using the default application for that file type on different operating systems? For example, when opening an image.png on Mac, it should open with Preview, and on Windows with Windows Photo Viewer. I know you can use open image.png ...

Are you conducting functional tests for Django on Travis?

Can Django Selenium tests be executed on Travis CI? Here are my test cases: class FrontendTest(unittest.TestCase): def setUp(self): self.browser = webdriver.Firefox() self.browser.implicitly_wait(3) def tearDown(self): se ...

Alert: Unrecognized custom element: <Document>

I am currently working on a Vuejs component that allows the user to input their organization and then pass that data to my view file. However, I encountered the following error: [Vue warn]: Unknown custom element: <Document> This is the code for ...

"JQuery event handlers not functioning as expected: .click and .on failing

For some time now, I've been facing this issue and I'm at a loss trying to figure it out. I have attempted various solutions such as using .click(), .on(), .delegate, and even utilizing .find() to locate the specific element causing the problem. ...

Exploring methods to deactivate specific dates within the angular material datepicker

Is it possible to prevent specific dates from being selected based on the current date? For example, if today is 8/1/16, I would like to disable the next 4 days (any number of days could be chosen), excluding weekends. So, if today is 8/1/16, I would want ...

Adding a class to a different UL tab from the tab div in jQuery tabs - a guide

Looking to implement a tabs navigation using jQuery without the jQuery Tabs UI. Essentially, when a user clicks on a list item, the script selects the list element with data-tab="X" and adds the class current, making the link visible (default op ...

Is there a way to shift an entire row to the right?

I need assistance with aligning a row of columns, each containing a button, to the center of the page. <div> <div class="container-fluid" id="home"> <h1>Welcome to Daniel's Portafolio</h1> <div class="row"> <div ...

Developing an interactive single-page website

My goal is to develop a PHP web application with an overlay container that features dynamic fields/forms. The idea is for users to fill out the form without being redirected from the main page they are on. Imagine being able to report a page error on a w ...

Enhance your color picker with advanced features such as selecting colors directly from your screen

I have created a color picker using react-color () but now I want to add extra functionality. Specifically, I would like the color picker to be able to choose colors from the screen by toggling a feature. I have come across some libraries that claim to off ...

What is the procedure for attaching console.log to "l" in vue.js?

The code in main.js includes the following: window.l = function () { } try { window.l = console.log.bind(console) } catch (e) { } It functions properly in non-Vue applications. However, when trying to use it in a Vue action/method like this: l("test") ...

Issue with loading Bootstrap modal in KnockoutJS

Currently, I am working on a project to develop a CRUD system using knockoutJS and fetching data through an AJAX call. While I am still in the process of implementing the add and delete functionalities, I am facing issues with my modal. The modal form does ...

Is it possible to include all visible content, even when scrolling, within the full page height?

My webpage contains content that exceeds the height of the window. I am looking for a way to retrieve the full height of my page using either jQuery or pure Javascript. Can anyone provide a solution? I have considered the following approach: $('body ...