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

  • SEARCH
  • Home
  • 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 7722791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:16:38+00:00 2026-06-01T04:16:38+00:00

I have an onclick event called in a PHP foreach loop for several items

  • 0

I have an onclick event called in a PHP foreach loop for several items whose author name I need to click. Only one name was getting generated, so I know that I must have a unique id for each HTML element. I already have a variable $objkey for a counter used in the loop so I appended it to the id.

<?php
//This is the item that appears in the loop. 
//Currently, clicking on each one that is generated only generates the first author name
//The correct author as passed by PHP displays in the loop.
//$closest is the value of the author's name.
echo 'Click name to add <span><button id="closest' . $objkey . '"  onClick="setAuthor()">' . $closest . '</button></span><br />';
?>

I have done a print_r('$objkey); and the appropriate value for the counter is getting passed along.

I want the value of this button element $closest to be passed to an input field. I have appended the variable to both the input’s id and name elements (not sure if that helps) (UPDATE: There is an input field for each of the authors that needs to get the value):

<?php 
echo '<input id="author_name' . $objkey . '"  type="text" name="author_name' . $objkey .  '" value="' . $author . '" />'; 
?>

Where I’m stuck is with my function:

<script type="text/javascript">function setAuthor() {
var author = document.getElementById("closest").innerHTML;
window.alert(author);
document.forms["myform"].elements["author_name"].value = author; }</script>

How do I create a variable in the function for the unique ids so that each looped item generates distinct names?

  • 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-06-01T04:16:40+00:00Added an answer on June 1, 2026 at 4:16 am

    One way to do it is to pass this to the the setAuthor function:

    <?php
    //This is the item that appears in the loop. 
    //Currently, clicking on each one that is generated only generates the first author name
    //The correct author as passed by PHP displays in the loop.
    //$closest is the value of the author's name.
    echo "Click name to add <button id='closest_{$objkey}' onClick='setAuthor(this)'>{$closest}</button><br/>";
    ?>
    

    With this approach, you can access the element within setAuthor:

    <script type="text/javascript">
    function setAuthor(el) {
        var author = el.innerHTML,
            id = el.id.replace(/^closest_/, '');
        if (console) {
            console.log(author);
        }
        document.forms["myform"].elements["author_name_" + id].value = author;
    }
    </script>
    

    However, you shouldn’t set onclick inline and for each element. You should use an event registration function.

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

Sidebar

Related Questions

I have an asp button with no onclick event but only onclientclick. This call
I have a div section in which I have an image whose onclick event
I have a function that gets called from an onclick event for example <a
I have the following scenario: At onClick() event 2 javascript functions are called, each
I have number of drop down menu's which gets called with a onClick event
I have a problem with <a> tags. I need to reassign an onclick event
I have a onclick event that is like: public void OnMyButton_Click(object sender, EventArgs e)
On Window 's load, every DD element inside Quote_App should have an onCLick event
I have a div which I have attached an onclick event to. in this
I have a gridview control that has on onclick event bound to every cell.

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.