I combined many things from what I read in forums and here is what I got so far:
$(document).ready(function () {
$('input[name=key]').keyup(performUsernameAjax).focusin(performUsernameAjax);
$('#users').click(function (event) {
console.log('hello');
event.preventDefault();
console.log($(this).text());
var dataStr = {
'username': $(this).text(),
'method': 'userdata'
};
var base_url = '<?= base_url() ?>';
var url = base_url + 'login/userdata';
console.log(url);
$.ajax({
type: 'POST',
data: dataStr,
dataType: 'json',
url: url,
success: function (data) {
$("#userInfo").append("<p><ul><h3> " + data.username + "'s Info:</h3>");
$("#userInfo").append('<li> The username: ' + data.username + '</li>');
$("#userInfo").append('<li> The full name: ' + data.fullname + '</li>');
$("#userInfo").append('<li> The num of failed logins: ' + data.failedlogin + '</li>');
$("#userInfo").append('</ul></p>');
}
});
return false; //for good measure
});
});
//part of the performUsernameAjax function
for (var i = 0; i < data.length; i++) {
console.log(data[i].username);
$("#usernameList").append("<p><a href='#' id='users'>" + data[i].username + "</a></p>");
}
HTML :
<div class="field-info" id="usernameList"> </div>
<br>
<p><div class="field-info" id="userInfo"> </div></p>
<p><a href="#" id="users">rotemel</a></p>
The "for" loop is supposed to create a list of hyperlinks with usernames and I want that whenever a link is clicked the "click" listener will "catch" it and will do whatever I do there.. But it doesn't work so I tried few things: when I append a link in the "for" loop like that:
$("#usernameList").append("<p><a href='http://www.google.com' id='users'>" +
data[i].username + "</a></p>");
the link itself works fine - it redirects to the google page but when I do this:
$("#usernameList").append("<p><a href='#' id='users'>" +
data[i].username + "</a></p>");
because I want it to go to the "click" listener function that I've created, it does nothing... I thought perhaps that it might be the "click" listener function's fault so I added in the html file:
<p><a href="#" id="users">rotemel</a></p>
and it worked perfectly! So I'm just lost... what is the problem with my append or my href?? Thanks in advance!