title: Hoe to get data based onclick in javascript
body: <p>Based on onClick function I want to get data for the particular user.
  What I tried to create a function getSelectedData and tried to called by passing current data as an argument.</p>
  <p>But received only the last value of the array.</p>
  <pre><code>function getSelectedData(receiveData){
    console.log(receiveData)
  }
  
  var card = document.getElementById('mainBody')
  
  var x = &quot;&quot;
  for(var i = 0; i &lt;= user.length -1; i++){
   var UserData = user[i]
      x = x +`
      &lt;div class=&quot;card m-2 d-flex flex-column align-items-center&quot; onclick=&quot;getSelectedData(UserData)&quot;&gt;
              &lt;img src=&quot;${user[i].userImage}&quot; alt=&quot;&quot; class=&quot;UserImage my-3&quot;/&gt;
              &lt;p class=&quot;userName&quot;&gt;${user[i].userName}&lt;/p&gt;
              &lt;p class=&quot;userCountry&quot;&gt;${user[i].userCountry}&lt;/p&gt;
              &lt;p class=&quot;userProfession&quot;&gt;${user[i].userProfession}&lt;/p&gt;
      &lt;/div&gt;` ;
  }
  card.innerHTML = x
  
  </code></pre>