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 7695131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:28:44+00:00 2026-05-31T21:28:44+00:00

Forgive me if this question has been asked before but I am new to

  • 0

Forgive me if this question has been asked before but I am new to jQuery and am having trouble even describing my problem. I have a loop in a php page that looks something like this:

foreach ($blahs as $blah) {
    echo '<p class="title">'.$blah->title.'</p>';
    echo '<input/>';
    echo '<input/>';
    echo '<div class="delete-button"><img /></div>';
}

What I am trying to accomplish is the replacement of .$blah->title. with the phrase “Delete this title?” in the closest preceding <p> element to whichever <div class="delete-button"> is clicked. To do this I have some jQuery at the top of the page like so:

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery(".delete-button").click(function() {
            jQuery(this).prev('.title').html("Delete this title?");
        });
    });
</script>

However, this does not work. If I remove the .prev('.title') it replaces the html of the div so I know its working on some level but I cannot manage to get it to replace the html of the previous <p class="title">. Thanks for your help.

  • 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-31T21:28:45+00:00Added an answer on May 31, 2026 at 9:28 pm

    you can take it a step further by wrapping all the items in a common parent and attach a single handler to the parent using .on() instead of assigning a handler per button. it’s a performance benefit to only have a single handler as opposed to having X number of handlers for X items in the list.

    also, seeing the structure of your items, let’s wrap them. it prevents all items from being siblings (thus making an item distinct). then we can just use the .sibling() method to find the title relative to the button.

    also, better use lists, they are more semantic.

    DEMO

    <ul class="items">
        <li>    
            <p class="title">Blah title</p>
            <input/><input/>
            <div class="delete-button">Delete Button</div>
        </li>
        <li>
            <p class="title">Blah title</p>
            <input/><input/>
            <div class="delete-button">Delete Button</div>
        </li>
        <li>
            <p class="title">Blah title</p>
            <input/><input/>
            <div class="delete-button">Delete Button</div>
        </li>
    </ul>
    

    ​

    $(function(){
    
        //add a single handler to the parent instead of per button
        $('.items').on('click','.delete-button',function(){
            //the "this" in here is the delete button element
            //since the button and .title are siblings, use the sibling method
            $(this).siblings('.title').text('Delete this title?');       
        });
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am aware that this question has been asked before, but I have tried
Forgive me if this question has already been asked, but... I'm trying to select
This is my first question, so forgive me if it has been asked before.
I know this question has been asked before but I'm confused as to the
Forgive me if this has already been asked, but I'm new to C# and
Forgive me if this has been asked before, but I was unable to find
Forgive me if this has been asked before; I did a search but couldn't
Forgive me if this is a repeated question but I am having trouble finding
Please forgive if this question has been asked numerous times. I recently installed Eclipse
Forgive me if this has been asked before, I'm just unable to find an

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.