JQuery Quick Tip: Custom Attributes

For quite some time,  whenever I needed to pass something like a database table row index from a webpage to a PHP script I would do string manipulation to get the data.

Something like this:

<div class="tdiv" id="td_4932">Something interesting</div>


var tid=this.id;

tid = tid.replace('td_','');

//now tid = the index



In an epic /facepalm moment I realized that I’ve been doing it the hard way.   I realized this when I had to pass several bits of information back to the PHP script.  The string manipulation code would be kludgetastic.

It was simple. Put the parameter as a custom attribute!

Like this:

<div class="tdiv" id="tdept" dept="4932">Something interesting</div>  //the dept attribute points to the target department in the database


var tid = $('#tdept').attr('dept');

console.log("The dept index is " + tid);



JQuery Quick Tip: Loop through each visible object


I need to loop through some checkboxes identified by a class.

There are lots of “try this” in the forums, so I wanted to post what worked in jquery 1.11.1

This iterates through objects of a class selector that are visible and concatenates their ID to a string.

….because I wanted the example to be simple,  that’s why.


$('.aacheck').filter(':visible').each(function(i,o) {    //i: iteration  o: object

chdept += '~' + $(o).attr('id');


If you need to check multiple conditions (like checkboxes that are visible AND checked)…

$('.aacheck').filter(':visible').filter(':checked').each(function(i,o) {    //i: iteration  o: object

chdept += '~' + $(o).attr('id');



PHP: Tracking session variable content while developing

I like using session variables to track various and sundry as users navigate through my sites.

The problem comes when I forget to change, or load a session variable.

What I used to do was add var_dump($_SESSION) lines to my scripts…but of course, then you have to go remove them.

This weekend I thought of a cleaner way.

I create a whole new file, usually sessvar.php. I then point my browser to it, and whenever I need to see what the session variables are, I simply refresh the page.

So, you’d navigate to yourpageurl/sessvar.php in a tab besides what ever you’re using to check your work, and refresh the sessvar.php page whenever you needed to see what variables your stored were.