0

I have an image inside a table cell which is also a clickable link with the following markup:

<a class="modalInput" rel="#flagsSummary" style="cursor:pointer">
    <img src="/FatcaOne_0/static/images/circleRed.png" width="20" height="20">
</a>

I am confused about the syntax of Javascript that I need to use just to change the source of the image or the image itself and leave the element and its attributes untouched. I tried this but that changes the link also which I don't want:

rows[lastRowClicked].cells[0].innerHTML = '<a class="modalInput" rel="#flagsSummary" style="cursor:pointer"><img src="/FatcaOne_0/static/images/circleYellow.png" width="20" height="20"></a>';

I only wish to change the image in the last row that was clicked.

2 Answers 2

3

Try something like this:

var cell = rows[lastRowClicked].cells[0];
var allImagesInCell = cell.getElementsByTagName('img');
var theImage = allImagesInCell[0];

theImage.setAttribute('src', '/some/new/value');

You can also use theImage = cell.querySelector('img') if you don't need to support IE8 and below (see here https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector#Browser_Compatibility).

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

Comments

3

You can search for the image element within the table cell and just change the .src property.

var cell = rows[lastRowClicked].cells[0];
cell.querySelector('img').src = 'some/other/image.jpg';

Comments

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.