Javascript Issue with aria-hidden popup

Viewed 1011

I have this ajax code that gets a res.json(event) from the server and then creates an object based on the value received.

Here is part of that code:

 html += `<div class="card-header" id="headingOne-${i}">` +
`<div class="event-time"><time class="published" datetime="2017-03-24T18:18">${data[i].events.targetReminder} | ${data[i].events.targetAmPM}</time><div class="more">   <svg class="olymp-three-dots-icon"><use xlink:href="svg-icons/sprites/icons.svg#olymp-three-dots-icon"></use>   </svg><ul class="more-dropdown"><li><a href="#" onClick="reply_click()" id ="mark-${i}">Mark as Completed</a></li>  <li><a href="#" id ="delet-${i}">Delete Event</a>   </li></ul></div></div>` +
`<h5 class="mb-0 title"><a href="" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne" >${data[i].events.title}<i class="fa fa-angle-down" aria-hidden="true"></i>` +
`   <span class="event-status-icon" data-toggle="modal" data-target="#public-event"><svg class="olymp-calendar-icon" data-toggle="tooltip" data-placement="top" id ="uncomplet-${i}"data-original-title="UNCOMPLETED"><use xlink:href="svg-icons/sprites/icons.svg#olymp-calendar-icon"></use></svg></span></a></h5></div>` + 
`<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#headingOne-${i}"><div class="card-body" id="${data[i].events._id}" onClick="reply_click()">${data[i].events.caption}</div><div class="place inline-items"><svg class="olymp-add-a-place-icon"><use xlink:href="svg-icons/sprites/icons.svg#olymp-add-a-place-icon"></use></svg><span>${data[i].events.location}</span></div></div></div>`;

And here is the output of the code:

enter image description here

The aria-hidden that I'm talking about is this one

<div class="modal fade" id="public-event" tabindex="-1" role="dialog" aria-labelledby="public-event" aria-hidden="true">

What I have tried and already know it's not a proper solution is here

Code test

I created a script that change aria-hidden => true or false but I don't know how to link it with my box

<script type="text/javascript">
    function reply_click()
    {

        document.getElementById('public-event').setAttribute('aria-hidden', 'false');
    }
  </script>

another failed try:

<script type="text/javascript">
    function reply_click()
    {
       alert('this function is called')
        $(`#mark-${i}`).onclick = function() {
            document.getElementById('public-event').setAttribute('aria-hidden', 'false');
        }; 

    }
  </script>

I also added in the HTML this function onClick="reply_click()" but nothing is happening. I only get the alert('this function is called')

Could you suggest me an idea, please?

2 Answers

could this be your problem ? you create an item dynamically with javascript, but the item you want to select, click event, is not actually created at that time. Once the item you want to export is created, you can select it and want to make the changes you want.

udpated: I added a snippet of what I meant. in order for me to select the H1 tag that occurs after I click on the button, I have to write a function that will occur after it occurs.

const button = document.querySelector(".clickme");
      const container = document.querySelector(".container");

      button.addEventListener("click", () => {
        container.innerHTML +=  `
        <h1 class = "change-modal"> ı cant select this</h1>
        <div class="modal fade" id="public-event" tabindex="-1" role="dialog" aria-labelledby="public-event"
        aria-hidden="true"> `
      })

        const changeModal = document.querySelector(".change-modal")
        console.log(changeModal)
      
 <button class="clickme">Click me</button>

    <div class="container" style="background-color: red;">
    </div>

    <h1 class="change-modal">ı select this</h1>

updated2 :

for (let i = 0; i < data.length; i++) {

      html += // your html

      $("eventCard").append(html);

    //after this, you can select the item and type the function because the items are created here.      

    }

Updated 3!!! : I explained how to do things with javascript without using jquery. Hopefully you know what I mean.

async getData() { // fetch operations using javascript
        const data = await fetch(url); // your api url
        const jsonToData = await data.json(); // here you can Request api and obtain the data
        return jsonToData;
    }

    getData().then((data) => {
        console.log(data) // ıts probably an array. 
        for (let data = 0; data < jsonToData.length; data++) {
            // data operations, what if you want to 
            html += // you printed document items,
        }
    })
    .then(() => {
        //!!! IMPORTANT!!!  this is where you need to perform the operation of selecting the element function.    you can also write a function that can work for the code here, but I've written it one by one for now.
       const clickedElement =  //type whatever element you want to click on. !!
        clickedElement.addEventListener("click", function() {
            const elementToChange = document.getElementById(".public-event");
            elementToChange.setAttribute('aria-hidden', 'false');
        })
    })
    .catch(err => console.log(err))

By looking at your question and discussion on the comment section I think that you are trying to add click event on your dynamically generated div section(html part) and open the pop up modal. We can achieve that by using $('#id' OR '.class').modal('show') in jquery. So why don't you add a class any where inside your div section of html and bind a click function using jquery. Suppose you have added a class name showModal on the very first div after card-header like, div="card-header showModal"

        $('#eventCard').on('click', '.showModal', function(){
                  $('#public-event').modal('show');
           });

We cannot directly use $('.showModal').click...... because document structure is change after appending the html section after #eventCard which was initially was not present. Hpe this works.

Related