javascript - Repeated elements' onclick event only works for first row -


below find code using pull data table along image want repeat each table row listed in last button tag class=plusbtn

{          $event .= '     <div  style="border-bottom:1px solid gray;padding:2px;width:100%;float:left;"><div id="eventslist" onclick="goevent('.$row['id'].');">                         <div style="width:100%;float:left;">                             <h4 style="margin:0;"><span id="eventuser" style="width:50%;float:left;text-align:left;font-size:14px;"><img src="https:///login/profile/'.$row['profile_img1'].'" style="width:35px;height:37px;border-radius:20px;margin-left:-4%;background:url(http:///img/person-placeholder.jpg) no-repeat center" class="img-rounded"> <b>'.$row['user'].'</b></span><span style="float:right;text-align:right;font-size:12px;margin-top:9px;margin-right:-25px;">posted: '.$row['stdate'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br/><br/><b><span style="font-size:20px;float:left;">'.$row['fname'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></b></h4>                         </div>                         <div style="width:109%;float:left;">                             <img src="http:///login/image/'.$row['name'].'" style="width:100%;height:35%;border-radius:10px;margin-left:-4%;background:url(https:///img/load.gif) no-repeat white" class="img-rounded">                         </div>                         <div style="width:100%;float:left;">                             <p style="margin-bottom:0;">'.substr($row['description'],0,110).'...</p>                         </div>                 </div><div><button class="plusbtn" onclick="plusrate(\''.$likecount.'\');"><a onclick="document.getelementbyid(`myimage`).src=`https:///img/fav4.png`"><img id="myimage" src="https:///img/fav3.png" style="opacity: 0.7;height:25px;max-width:100px"></a></button><span id="likeqnty" class="likeqnty">0</span> <b>likes</b></div></div>   ';     } 

using set button image appears every entry can click button @ first entry on page. when type echo around button (echo"button") appears on page

how can make button repeat every table entry , work correctly switching fav4 fav3.

you problem stands in fact ids should unique. images have same id.

<a onclick="document.getelementbyid(`myimage`).src=`https:///img/fav4.png`">     <img id="myimage" src="https:///img/fav3.png" style="opacity:0.7;height:25px;max-width:100px"> </a> 

clicking on of links change first image id myimage.

you'd change onclick javascript target image respectively current click target.

<a onclick="this.getelementsbytagname(`img`)[0].src=`https:///img/fav4.png`">     <img id="myimage" src="https:///img/fav3.png" style="opacity:0.7;height:25px;max-width:100px"> </a> 

in code, use current dom element received click. , retrieve image inside it, , change src value. makes sure targeted image relative user clicked.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -