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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:46:25+00:00 2026-06-15T02:46:25+00:00

I’m trying to make a list of items editable and update with AJAX using

  • 0

I’m trying to make a list of items editable and update with AJAX using jQuery.

HTML

<ul>
   <li data-id="1">
       <span class="title">Title 1</span><span><a href="#" class="changeLink">Change</a></span>
   </li>
   <li data-id="2">
       <span class="title">Title 2</span><span><a href="#" class="changeLink">Change</a></span>
   </li>
   <li data-id="3">
       <span class="title">Title 3</span><span><a href="#" class="changeLink">Change</a></span>
   </li>
</ul>

jQuery

$('a.changeLink').on('click', function(e) 
{
    e.preventDefault();
    // get parent LI element
    var li = $(this).closest('li'); 
    // catch current title
    var title = li.children('span.title').text(); 
    // replace span
    li.children('span.title').html('<input type="text" value="'+title+'" />');
    // change the link class and label 
    $(this).removeClass('changeLink').addClass('saveLink').text('Save'); 
});

$('a.saveLink').on('click', function(e) 
{
    e.preventDefault();
    // get parent LI element
    var li = $(this).closest('li'); 
    // get the id for the AJAX query
    var id = li.attr('data-id'); 
    // get the input value
    var title = li.children('span.title input').val(); 
    // restore span text with new
    li.children('span.title').text(title); 
    // restore link
    $(this).removeClass('saveLink').addClass('changeLink').text('Change'); 
    // make AJAX post to save new title
    $.post('updatetitle.php',{ 'id': id, 'title': title }, function(){ alert('OK'); }); 
});

My problem seem to be that the saveLink click event never occurs. Also the changeLink click event seem to happen more than once. Why does this happen – the link don’t have the changeLink class anymore?

I put this in a jsFiddle here: http://jsfiddle.net/jtheman/SZk4h/1/

Glad if I can get some input, what is my error?

  • 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-15T02:46:26+00:00Added an answer on June 15, 2026 at 2:46 am

    The event will be associated to an element and not specifically to a class..

    If you want to work it with class you might need to delegate the event

    UPDATED CODE

    $('ul#list').on('click', 'a.changeLink , a.saveLink', function(e) {
        e.preventDefault();
        if ($(this).hasClass('changeLink')) {
            var li = $(this).closest('li');
            var title = li.children('span.title').text();
            li.children('span.title')
                .html('<input type="text" value="' + title + '" />');
            $(this).removeClass('changeLink')
                   .addClass('saveLink').text('Save');
        }
        else if ($(this).hasClass('saveLink')) {
            var li = $(this).closest('li');
            var id = li.attr('data-id');
            var title = li.children('span.title').find('input').val();
            li.children('span.title').text(title);
            $(this).removeClass('saveLink')
                   .addClass('changeLink').text('Change');
            //$.post('updatetitle.php',
            // { 'id': id, 'title': title }, function(){ alert('OK'); });
        }
    });​
    

    Your problem was this line

    var title = li.children('span.title input').val();
    

    .children() will only get the immediate children of the element.

    Replace it with

    var title = li.children('span.title').find('input').val();
    

    Check Updated Fiddle

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.