-1

I have a modal and I want to choose an image from this modal and when I click "ekle" button I want it to be shown in another div. I'm trying to do this with innerHTML but image src is undefined. Is there a better way then innerHTML? If there is no better way how can I fix this undefined problem.

function testFunction() {
    var imagesrc = document.getElementsByClassName("selected").src;
    document.getElementById("deneme").innerHTML = '<img src="' + imagesrc + '" >';
}
<div class="modal fade" id="hazirresim" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Kategori Hazır Resimleri</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <input type="image" src="images/lol-1.jpg" class="selectable" height="250" width="250" style="padding-right: 3px;padding-bottom: 3px;">
                <input type="image" src="images/lol-1.jpg" class="selectable" height="250" width="250" style="padding-right: 3px;padding-bottom: 3px;">
                <input type="image" src="images/lol-1.jpg" class="selectable" height="250" width="250" style="padding-right: 3px;padding-bottom: 3px;">
                <input type="image" src="images/lol-1.jpg" class="selectable" height="250" width="250" style="padding-right: 3px;padding-bottom: 3px;">
                <input type="image" src="images/lol-1.jpg" class="selectable" height="250" width="250" style="padding-right: 3px;padding-bottom: 3px;">
                <input type="image" src="images/lol-1.jpg" class="selectable" height="250" width="250" style="padding-right: 3px;padding-bottom: 3px;">
            </div>
            <div class="modal-footer">
                <button onclick="testFunction();" type="button" class="btn btn-info btn-lg btn-block">EKLE</button>
            </div>
        </div>
    </div>
</div>
SorryBaby
  • 17
  • 4
  • 1
    1. There is nothing with the class "selected" in your code. 2. [What do querySelectorAll and getElementsBy* methods return?](https://stackoverflow.com/q/10693845) – VLAZ Jan 25 '22 at 11:51
  • when u select an image it is added to it's class list – SorryBaby Jan 25 '22 at 11:57
  • This is not reflected in the code you posted. Still, that leaves you with problem 2, in that case. – VLAZ Jan 25 '22 at 11:58
  • And also, `getElementsByClassName()` function returns an array and it can not have the property src, so we'll have iterate the array in a for loop or take only one of its index element. – Ayush Jan 25 '22 at 12:02

1 Answers1

0

Document.getElementsByClassName() return the array which has selected class. so you need [0].src for get first element src value

Replace document.getElementsByClassName("selected").src to document.getElementsByClassName("selected")[0].src this

 function testFunction() {
      var imagesrc = document.getElementsByClassName("selected")[0].src;
      document.getElementById("deneme").innerHTML = '<img src="' + imagesrc + '" >';
    }
Mayur Vaghasiya
  • 1,383
  • 2
  • 12
  • 24