A
A
Alexander2017-02-25 18:01:18
React
Alexander, 2017-02-25 18:01:18

How to get element id (variable i)?

Subject.
You need to somehow determine by the onCLick function which element the delete icon (span) belongs to in order to form a request to localStorage.

if(localStorage.length == 0)return(notesTemplate = <div>Заметки пусты </div> );
    else{
        for(var i = 0; i < localStorage.length; i++){
          var readObj = JSON.parse(localStorage.getItem("Note " + i));
          notesTemplate.push(
            <div key = {i} className="wrap">
                <div
                className={readObj.isComplete + ' note'}
                onClick={self.changeStateOfComplete}>
                              {readObj.note}
                </div>
                <span onClick={self.deleteNote} className={"close"}>X</span> // вот это место
            </div>
          );
         }
      }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2017-02-25
@aleksand44

I'd do it with the data-* attribute, but there was some discussion in this answer , so take your pick.

..
deleteNote(e) {
  console.log(e.target.dataset.item) //здесь будет идентификатор элемента
}
...
<span onClick={self.deleteNote} data-item={i} className={"close"}>X</span>
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question