Javascript functions and php foreach values

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
User avatar
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Tue May 08, 2012 6:55 am

The problem with your code is that you have the same ID for all of your check boxes, so only the first occurrence of that ID is being read by your JavaScript function.

Also, I have no idea what you are doing with the value of "check".

You need to change this portion of your foreach loop:

Code: Select all

echo "<td class='b'><input type='checkbox' id='chk_Bx' value=". $addrss_owner. " onClick='selectFunction()'></input></td>";
To this:

Code: Select all

echo "<td class='b'><input type='checkbox' value=". $addrss_owner. " onClick='selectFunction(this)'></input></td>";

Then you can change your JS function to this:

Code: Select all

function selectFunction(item){
var check = item.checked;
var val = item.value;

User avatar
php-forum Active User
php-forum Active User
Posts: 365
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Tue May 08, 2012 8:09 am

agreed, ids for elements should be unique to the page. If it's non-unique, use a class. Otherwise stick an incrementing value into your id if you have to use one.

Post Reply