1

I would like for a user to click an image in this table which is created dynamically based on the JSON data sent from the web service, and have the image change. When clicking the image again it will change back to the first image (inter-changing between only two images).

I have a table being created via jQuery's $.ajax() function which looks like this:

<table border="0" width=80% id="table">
            <thead>
            <tr>
                  <td><h3>Check to Renew</h3></td>
                  <td width=40%><h3>Vendor Part</h3></td>
                  <td width=100%><h3>Part Description</h3></td>
                  <td><h3>Unit Price</h3></td>
                  <td><h3>Quantity</h3></td>
            </tr>
            </thead>
            <tbody>
                <tr class="template">
                  <td><img src="images/checkmark.png" alt="check" id="row1" onclick=""></td>
                  <td>$vendorPart</td>
                  <td>$partDescription</td>
                  <td>$price</td>
                  <td><input class="quantityClass" name="quantity" value="$quantity"/></td>
                </tr>
            </tbody>
        </table>

Here is the simple Javascript function which changes the images:

renewimg=Array("images/checkmark.png","images/gray_x.png");
function rowImageRefresh(y)
{
    document.getElementById(y).src=renewimg[++m];
    if (m==1)
    {m=-1;}
}

This Javascript function work's beautifully, however only if I pass it the images id (in this case specific to the row). Hard coding for testing purposes proved this. My issue is I would like to be able to create a row id on the fly as the table row is created and then have functionality where that id can be passed.

I guess if I were to illustrate this more it would look something like this:

JavaScript: var row = 1;

HTML:
//table data

<td><img src="images/checkmark.png" alt="check" id="row+[i];i++;" onclick="rowImageRefresh(this.row.id)"></td>
//more table data

Where the id is created dynamically on the fly as each row is created and the onclick function passes the row of the image clicked.

4 Answers 4

1

You don't actually need an id at all. For example:

<img src="images/checkmark.png" alt="check" onclick="toggle(this)">

Then the script:

function toggle(element)
{
    var sources = ["images/checkmark.png", "images/gray_x.png"],
        current = $(element).data('state') || 0;

    current = 1 - current;

    $(element).data('state', current);
    element.src = sources[current];
}

It toggles between the two states, remembering the current state using .data().

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

Comments

1

You could set individual row id's with id="x". Or, you could just use jQuery to find the index from where the click event occured and get the index of that part.

$('img.check').click(function(){
   var id = $(this).index();
   rowImageRefresh();
});

2 Comments

So what your saying is I can just set the html to look like this: <img src="images/checkmark.png" alt="check" id="x" />, and then when the image is clicked it will hit that jquery function? I am wondering what do I send to rowImageRefresh since it changes the img src based on the element ID
This did not work for me, in fact it seems it did not even register as a click event.
1

Here's one way you might assign an id to each image based on its row index:

$.each(rows, function(index, row) {
    var data = $.extend({}, row, { id: "row" + index });
    var $row = $(template(data));
    $row.removeClass('template');
    var $img = $('img', $row);
    $img.on('click', function() {
        var $this = $(this);
        if ($this.data('checked') === true) {
            var checked   = $this.attr('src');
            var unchecked = $this.data('src');
            $this.attr('src', unchecked);
            $this.data('src', checked);
            $this.data('checked', false);
        } else {
            var unchecked = $this.attr('src');
            var checked   = $this.data('src');
            $this.attr('src', checked);
            $this.data('src', unchecked);
            $this.data('checked', true);
        }
    });

    $tbody.append($row);
});

The HTML for the image would look like this:

<img src="checked.png" alt="check" data-checked="true" data-src="unchecked.png" id="${ id }" />

Here's a working example: http://jsfiddle.net/potatosalad/MJYpA/1/

I used lodash.js for the template, but whatever you're doing to generate the row HTML should work the same.

Comments

1

Consider using a custom data attribute instead of trying to generate unique ids.

<img src="..." data-partid="$vendorPart" class="part-img" />

$(".part-img").on("click", function() {
    var id = $(this).data("partid");
    // or
    var id = $(this).attr("data-partid").val();

    rowImageRefresh(id);
});

Edit: Seems like you are trying to toggle a checkbox image. A better way might be to change the background image using css and sprites. Then on click you swap the class depending on state.

1 Comment

Hey Jasen, this answer makes a lot of sense to me. I think I may be doing something wrong however since the on click may not get called. Do I just place the jquery code inside the script tag within the head of this html page?

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.