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?
You could use the
contextparameter 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:
Alright, now that we have cleaned up the markup let’s subscribe unobtrusively to the
.click()event of this<li>: