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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:34:14+00:00 2026-06-06T18:34:14+00:00

I want to find an html element by its value. I tried with id,

  • 0

I want to find an html element by its value. I tried with id, but my case is fragile:

pseudo code:

for user in users:
 <li id="followed" onclick="follow($(this).text())"><a class="follow">user.name</a></li>
endfor

I want that each username will be click-able and I’m saving him into DB and appending “saved” to the end part of username. like this:

"username" ==> after click: "username saved"

I’m doing this through ajax.

function follow(data){
    var Him = data;
    alert(Him);
    $.ajax({
        url: "/follow",
        type: "POST",
        datatype: "html",
        data: {Him: Him}
    }).success(function(response){
        $('#followed').append(response);
    });
}

This code is fine, but it is appending the “saved” response only to the first username, because by the end of the loop, all usernames have id='followed'.

That’s why, I want to find the html element by its value. e.g. “username”.
Is it possible?

  • 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-06T18:34:15+00:00Added an answer on June 6, 2026 at 6:34 pm

    You could use the context parameter to change the context passed to the success callback of the AJAX request.

    But first let’s start by cleaning your markup and using a class name instead of id if this is a loop because as you know ids must be unique in HTML:

    for user in users:
        <li class="followed"><a class="follow">user.name</a></li>
    endfor
    

    Alright, now that we have cleaned up the markup let’s subscribe unobtrusively to the .click() event of this <li>:

    $(function() {
        $('.followed').click(function() {
            // Remark: maybe you wanna get just the username inside the anchor in which case
            // you probably need "var Him = $('a', this).text();"
            var him = $(this).text();
            $.ajax({
                url: '/follow',
                type: 'POST',
                dataType: 'html',
                context: this,    // <!-- Here, that's the important bit
                data: { him: him },
            }).success(function(response) {
                // since we have used the context, here 'this' will no
                // longer refer to the XHR object (which is the default) but
                // to whatever we have passed as context (in our case this
                // happens to be the <li> that was clicked) => we can be certain
                // that we are updating the proper DOM element
                $(this).append(response);
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often find that I want an element to adjust its width to the
I have a html code like <div> <span>TV</span> </div> I want to find this
I want to find and replace (from an html page) all occurrences of src
HTML: <table> <tr> <td> <a href=#>link</a> </td> </tr> </table> I want to: FIND 'link'
HTML: <table> <tr> <td> <a href=# class=nav>link</a> <td> </tr> </table> I want to: FIND
I didn't find it on their doc http://www.sqlite.org/lang_corefunc.html So just want to get confirm
I want to find a struct in a vector, but I'm having some troubles.
I want to find out the position of an element which is in iframe.
I want to find the Xelement attribute.value which children have a concrete attribute.value. string
Dont worry we dont need to find out any bug in this code. Its

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.