How can I correct a bug in the swap image jQuery code?


Code Fighter

I'm trying to create code to swap images in a table based on values ​​in the database. After doing some research on the site, I have this code, modified for my needs. It is not dynamic at this time, because the input data at this time is hardcoded. jQuery doesn't work because I'm missing some point. Very new to jQuery so if you could help point me in the right direction it would be greatly appreciated.

<table>
  <tr>
    <td>6</td>
    <td id="td1">
      <img />
      </td>
    </tr>
  </table>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script>
      $('#td1 img').attr('src' function(){
          
return parseInt($(this).parent().prev().html()) > 5 ? 'src1','/images/clubsTiny.png' : 'src2','/images/blkCross.png';
});
  </script>
Alireza Ahmadi

First $('#td1 img').attr('src', function ()by ,. Note attrthat two parameters are required, each with ,.

Then change the condition:

return parseInt($(this).parent().prev().html()) > 5 ? 'src1/images/clubsTiny.png' : 'src2/images/blkCross.png';

Note that the ternary condition isexpression ? 'do true one' : 'do false one'

 $('#td1 img').attr('src', function () {
            return parseInt($(this).parent().prev().html()) > 5 ? 'src1/images/clubsTiny.png' : 'src2/images/blkCross.png';
        });
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  
<table>
  <tr>
    <td>6</td>
    <td id="td1">
      <img />
      </td>
    </tr>
</table>

renew

For replacement content imgyou can use$('#td1').html(img)

function replace() {
    let td = $('#td1');

    let img = $("<img />");

    img.attr("src", ()=> +td.html() > 5 ? 'src1/images/clubsTiny.png' : 'src2/images/blkCross.png')

    $('#td1').html(img)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<table>
    <tr>
        <td id="td1">
            6
        </td>
    </tr>
</table>

<button onclick="replace()">click to replace</button>

Related


How can I fix the bug in this DirectX code?

Seraphim First of all, I'm not good enough at programming, so please forgive me if I can't solve trivial problems. I want to show a basic screen in a window, so I made a frame from 3 different sources, because I want to write it step by step to fully understan

How can I fix the bug in this DirectX code?

Seraphim First of all, I'm not good enough at programming, so please forgive me if I can't solve trivial problems. I want to show a basic screen in a window, so I made a frame from 3 different sources because I want to write it step by step to fully understand

How can I swap an image with another image after clicking on it?

Associated Press Some background on what I'm trying to do: I'm making a page with 4 icons/imgs at the top and when each icon is clicked something is displayed below it. Users can click any icon to hide the current one and show content related to the icon they

How can I turn the image in the correct orientation?

question_mark_77 I have a sheet of paper with scans of documents on it, I use tesseract to recognize the text, but sometimes the image is in the wrong orientation, then I cut these documents from the sheet and process each document individually, but I need to

How can I correct this code to make it generic?

Sebastian U I'm starting to learn coding, but I don't know how to generate a generic CRUD Read method. For testing, I assign the object to be of type Person, where the ID of the category is ID. The problem is that when comparing the properties sent by the read

How can I correct this code to make it generic?

Sebastian U I'm starting to learn coding, but I don't know how to generate a generic CRUD Read method. For testing, I assign the object to be of type Person, where the ID of the category is ID. The problem is that when comparing the properties sent by the read

How can I correct my Java code?

username I wrote this Java program that resizes and displays the image in C:/ and closes it within five seconds of display. Although the class compiles correctly, I can't close it (without hiding it) after five seconds. Here is the code: import java.awt.*; imp

How can I correct this code to make it generic?

Sebastian U I'm starting to learn coding, but I don't know how to generate a generic CRUD Read method. For testing, I assign the object to be of type Person, where the ID of the category is ID. The problem is that when comparing the properties sent by the read

How can I correct my Java code?

username I wrote this Java program that resizes and displays the image in C:/ and closes it within five seconds of display. Although the class compiles correctly, I can't close it (without hiding it) after five seconds. Here is the code: import java.awt.*; imp

How can I make this code output "after swap: 10, 5"?

Rocket So I am new to programming. While searching for different uses of void in C, I found this code from Quora. I first assumed it would produce the following results: "Before swap: 5, 10 After swap: 10, 5" But the output I get is: "Before Swap: 5, 10 After

How can I make this code output "after swap: 10, 5"?

Rocket So I am new to programming. While searching for different uses of void in C, I found this code from Quora. I first assumed it would produce the following results: "Before swap: 5, 10 After swap: 10, 5" But the output I get is: "Before Swap: 5, 10 After

How can I swap levels?

robertspierre I have the following code in R: a <- c("No", "Yes", "No", "Yes", "Yes") af <- as.factor(a) Then levels(af) return the goods [1] "No" "Yes" The problem is caret::confusionMatrixthat if you don't pass an positiveargument , the first factor is con

How to make this swap image code work in Wordpress

Aohor Arsalan Ok, I created a portfolio page based on this nice tutorial : http://designshack.net/articles/css/swap-your-pages-background-image-on-navigation-hover/ But because it's based on CSS, there are some limitations. Like currently, no image shows unles

How to make this swap image code work in Wordpress

Aohor Arsalan Ok, I created a portfolio page based on this nice tutorial : http://designshack.net/articles/css/swap-your-pages-background-image-on-navigation-hover/ But because it's based on CSS, there are some limitations. Like currently, no image shows unles

How can I save the image result of this code?

Derivatives I am trying to write a code to identify the eyes of a parakeet. So far I've managed to use the code that identifies the edges of the circles and get good results at a certain threshold. But I can't save the resulting image. I've tried imwrite('resu

How can I save the image result of this code?

Derivatives I am trying to write a code to identify the eyes of a parakeet. So far I've managed to use the code that identifies the edges of the circles and get good results at a certain threshold. But I can't save the resulting image. I've tried imwrite('resu

How can I echo this image code in php?

webmaster I want to echo this image in php echo code <p style="border-bottom:#666 dotted 1px;"><? echo '<a style="text-decoration:none" href="' . $site . $url . '">' ;?><img style="border:#2489ce solid 1px;" src="http://www.balkanweb.com/foto/<?=$vlerat['Id_F

How can I echo this image code in php?

webmaster I want to echo this image in php echo code <p style="border-bottom:#666 dotted 1px;"><? echo '<a style="text-decoration:none" href="' . $site . $url . '">' ;?><img style="border:#2489ce solid 1px;" src="http://www.balkanweb.com/foto/<?=$vlerat['Id_F

How can I correct this z-index and button image?

eleven So I'm new to coding and I've been playing around with it for the past two days and I've got this setup so far... This is a re-edit of my original post. notes: i(br) makes the body roll temporarily. Otherwise, the code is exactly what I want. I read a b