I want to create a clickable image using Vue.js

Whenever I click on the image, I aim to apply a particular class to it. Here is how I am currently trying to accomplish this:

<div class="thumbnail">
    <img 
        :image_id="image.id" 
        :src="'/storage/images/'+image.name"
        @click="select(image)"
        :class="{
           'selected': image.selected
        }"
    />
</div>


select(image) {
     for (let i = 0; i < this.imageData.length; i++) {
         if(image.id == this.imageData[i].id) {
             this.imageData[i].selected = true
          }
     }
}

The issue I am facing is that upon clicking, the method is not being triggered. To confirm this, I added console.log() in the select() method.

I would appreciate any suggestions or solutions to address this problem.

Answer №1

To enable the select method in your script, make sure to register it as shown below:

<script>
export default {
 methods: {
   select(item) {
     for (let i = 0; i < this.itemsData.length; i++) {
         if(item.id == this.itemsData[i].id) {
             this.itemsData[i].selected = true
          }
     }
   }
 }
}
</script>

Answer №2

A helpful resolution for displaying a single image can be found here: https://jsfiddle.net/vqzyx6hn/

<div id="app">
 <div class="thumbnail">
    <img 
        :image_id="picture.id" 
        src="http://farm1.static.flickr.com/47/139324914_374969bd81.jpg"
        @click="choose(picture)"
        :class="{
           'selected': picture.selected
        }"
    />
</div>
</div>
new Vue({
  el: "#app",
  data: {
    picture : {
        id: 1,
      selected: false,

    }
  },
  methods: {
    choose: function(picture){
    alert('This method is operational')
    }
  }
})

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

Deliver an extensive JSON reply through a Node.js Express API

When dealing with a controller in a node/express API that generates large data sets for reports, reaching sizes as big as 20Mb per request, maintaining a positive user experience becomes essential. What strategies can be employed to efficiently handle suc ...

Returning the user to the previous page after successfully submitting a web form on the current page in a .NET environment

Is there a simple way on a .net website to navigate a user back to the previous page they were on before submitting a form without needing a complex breadcrumb system? For example, imagine moving from Page1.aspx to Page2.aspx, which contains a form. Upon f ...

Steps for adding an HTML string to a div element using Ajax

I am facing some major challenges with Ajax, especially when it comes to appending HTML code to a div. I am attempting to append this HTML string to <div id="content-loader"></div> PHP function getLogo(){ $logo = '<div class="bg- ...

Addressing the problem of idle state with the idle-vue plugin

I have integrated the idle-vue plugin to automatically log out logged-in users if they are inactive for more than 10 minutes, accompanied by a pop-up message. While following instructions from https://medium.com/js-dojo/how-to-set-timer-idle-in-vue-1f4b57 ...

Rails offers a unique hybrid approach that falls between Ember and traditional JavaScript responses

My current project is a standard rails application that has primarily utilized HTML without any AJAX. However, I am planning to gradually incorporate "remote" links and support for JS responses to improve the user experience. While I acknowledge that gener ...

Encountering an error in AngularJS: Issue with require(...) function, along with a runtime error in Node

I have been working on a code similar to the one available here However, when I try to run node web.js, I encounter a TypeError: require(...) is not a function What could be causing this error? Where might the issue lie? Below is my current web.js set ...

How can one use C# and Selenium to send text to a hidden textarea with the attribute style="display: none;"?

I'm encountering an issue where I am unable to write in the textarea using the sendkeys function in Selenium. The specific textarea I am trying to target has an ID of 'txtSkillsTaught-Value' and is located after a script tag that seems to be ...

Regex can present two potential scenarios

I need to extract numbers from two different cases of a string. The first case is <@&!302050872383242240> And the second case is <@&302050872383242240> Is there a way to extract only the numbers from this string or remove the elemen ...

Adjusting the React Material UI TextField behavior upon a change in value while maintaining the

I have an MUI TextField component that I would like to trigger a function when it changes, while still allowing it to function as usual (without being a controlled input). <TextField onChange={(e)=>{ doSomething(e.target.value) //perhaps call ...

The Facebook share button on my website is malfunctioning

Could someone please help me figure out why my custom Facebook share button is not functioning properly? When I click the button, the share window fails to appear. I have tested it as an html file on a live web server and have thoroughly reviewed the Faceb ...

Unleashing the potential of an endless animation by incorporating pauses between each iteration

I am trying to create an infinite animation using animate css but I want to add a delay between each iteration. After exploring various options, I first attempted to achieve this using plain JavaScript. Here is the HTML snippet: <div id="item" class= ...

:The computed property in Vue is not being invoked by the class

Code:- <!--:class="changeCalmarColor" is not working--> <div :class="changeCalmarColor" class="content" > <div v-if="props.currency" :class="[ 'font-bol ...

Tips for modifying `sourceMappingURL` in parcel js

Is there a way to manually adjust the path of //# sourceMappingURL in the generated bundle.js? The current configuration in Parcel is causing the path to be incorrect for bundle.js.map. Parcel setup: "scripts": { "watch:js": &quo ...

When a form added by jQuery is submitted, the associated JavaScript does not run as expected

After clicking a button on my page, jQuery removes one form and replaces it with another. However, when I try to submit the second form, the associated JavaScript does not execute as expected. Instead of running the JavaScript function, the form submissio ...

What is the best way to access data from outside a forEach loop in JavaScript?

I am trying to access the value of my uid outside of the foreach loop in my code. Can anyone assist me with this? This is the code I am working with: var conf_url = "https://192.168.236.33/confbridge_participants/conference_participants.json?cid=009000 ...

Switching a material-ui input control instead of a textfield for materials-ui-datepicker: the ultimate guide

Within my React application (v16.3), I am utilizing the DatePicker component from the material-ui-pickers library to render date-picker controls. This particular component renders a Material-UI TextField. However, I am interested in modifying it to only di ...

Tips for centralizing error handling in Vue.js components

Within my component, I frequently use axios with then().catch() where I always include console.error() in the catch block like this: axios.get( //... ).then( //... ).catch( error => { console.error(..) } ) In addition to these instances, there a ...

What steps can be taken to provide accurate information in the absence of ImageData?

Utilizing a JS library to convert a raster image into a vector, I encountered an issue where the library returned a fill of solid color instead of the desired outcome. I suspect that the problem lies within the ArrayBuffer. The process of loading an imag ...

Tips for making WebDriver pause until Sencha AJAX request finishes

While testing a page with Selenium WebDriver, I encountered an issue related to the Sencha JavaScript library being used on the underlying page. The problem arises when I input a value into a field and an AJAX call is made to validate that field. If the va ...

How to send responses with parameters in Node.js for .js files

I am having some difficulty with my node.js http application. While I am able to properly handle GET requests, I am struggling to respond correctly to requests for files like foo.js?_param=1234. How should I handle files of this type which have parameters ...