Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1085113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:38:49+00:00 2026-05-16T22:38:49+00:00

Hey everyone, take a look at the code below and appreciate how messy the

  • 0

Hey everyone, take a look at the code below and appreciate how messy the id attributes are

View File

<?php foreach($array_project as $prj) : ?>
   <div id="prj-p<?=$item['project_id'] ?>">
    <?php foreach($arr_skill as $skill) : ?>
        <h2><?=$skill['name'] ?></h2>
        <a class="view" id="skill-p<?=$prj['project_id'] ?>-s<?=skill['skill_id'] ?>">view</a>
        <a class="edit" id="edit-p<?=$prj['project_id'] ?>-s<?=skill['skill_id'] ?>">edit</a>
        <a class="delete" id="delete-p<?=$prj['project_id'] ?>-s<?=skill['skill_id'] ?>">delete</a>
    <?php endforeach; ?>
   </div>
<?php endforeach; ?>

Javascript File (using Jquery)

$('.view').live('click', onClick);
$('.edit').live('click', onClick);
$('.delete').live('click', onClick);

function onClick()
{
    // prjId and skillId are effectively arguments that are
    // traditionally passed via onClick(prjId, skillId), but here
    // we've attached them to element ids
    prjId = this.id.replace(/(skill\-p)|(\-s\d+)/g, '')
    skillId = this.id.replace(/(skill\-p\d+)|(\-s)/,'');

    // do stuff with the prjId and skillId
}

So my issue with the above code is that doing something like this in the view file

<a class="view" id="skill-p<?=$prj['project_id'] ?>-s<?=skill['skill_id'] ?>">view</a>

is effectively the same as

<a onclick="onClick(<?=$prj['project_id'] ?>,<?=skill['skill_id'] ?>)">view</a>

With the latter actually being more readable to the programmer. In the former, I don’t like how I have to derive my own id naming convention to keep track of database entity ids: for example, -p prefix denotes project id, and -s prefix denotes skill_id. And then I have to use regular expression to parse it. I don’t liek the latter of inline js event handlers, because that’s intrusive javascript.

I thought about simplifying the code like this:

View File

<?php foreach($array_project as $prj) : ?>
   <div id="prj-p<?=$item['project_id'] ?>">
    <?php foreach($arr_skill as $skill) : ?>
        <h2><?=$skill['name'] ?></h2>
        <input type="hidden" class="project_id" value="<?=$prj['project_id'] ?>" />
        <input type="hidden" class="skill_id" value="<?=$prj['skill_id'] ?>" />
        <a class="view">view</a>
        <a class="edit">edit</a>
        <a class="delete">delete</a>
    <?php endforeach; ?>
   </div>
<?php endforeach; ?>

Javascript File (using Jquery)

$('.view').live('click', onClick);
$('.edit').live('click', onClick);
$('.delete').live('click', onClick);

function onClick()
{
    prjId = this.parentNode.childNodes[1].value;
    skillId = this.parentNode.childNodes[2].value;

    // do stuff with the prjId and skillId
}

This is much easier less coding when I have A LOT of db entity ids to reference between the js and view files (eg. i only have to print the project_id and skill_id ONCE). But the problem with this solution is that as soon as my designer changes the xhtml schema, I have to update my javascript file to re-reference the hidden input fields.

Is there an easier and less code-redundant way for html elements to pass data to javascript functions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:38:50+00:00Added an answer on May 16, 2026 at 10:38 pm

    You could use data attributes, like this:

    <?php foreach($array_project as $prj) : ?>
       <div id="prj-p<?=$item['project_id'] ?>" data-pid="<?=$prj['project_id'] ?>" data-sid="<?=$prj['skill_id'] ?>">
        <?php foreach($arr_skill as $skill) : ?>
            <h2><?=$skill['name'] ?></h2>
            <a class="view">view</a>
            <a class="edit">edit</a>
            <a class="delete">delete</a>
        <?php endforeach; ?>
       </div>
    <?php endforeach; ?>
    

    Then access them in jQuery:

    $('.view, .edit, .delete').live('click', onClick);
    
    function onClick()
    {
        var div = $(this).closest("div"), 
            prjId = div.attr("data-pid"),
            skillId = div.attr("data-did");
    
        // do stuff with the prjId and skillId
    }
    

    This work in HTML4 and is a part of the HTML5 standard, so no conflicts now, completely compliant HTML later. with this approach you may not even needs the id on the <div>, if that’s the case you can remove the attribute, since it’s not needed, only the data- ones are used in the onClick() function above. You may also want to give that container <div> a class, like class="project" and change the call to find it to .closest(".project") to make it a bit more resilient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone... I'm not too familiar with PHP... but I have PHP code that
Hey everyone, I'm working on a PHP application that needs to parse a .tpl
Hey everyone, in the following code, what should the result of d be after
Hey everyone. While I'm trying to learn some PHP and mySQL, I ran into
Hey everyone! I'm building a webapp in PHP that uses a MySql DB. What
Hey everyone. I have an exe file that runs a console program that was
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey everyone, I am trying to run the following program, but am getting a
Hey everyone. I'm trying to make a swing GUI with a button and a
Hey everyone. I am having some issues on my end and hopefully it's just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.