I have a small question. I have an input field where somebody can search a specific value. Here is the following code
$('#usersearch').keyup(function () {
var inputvalue = $('#usersearch').val();
$('#usersearch').val(inputvalue);
document.getElementById("popupwithuserssearch").style.display = "block";
$.ajax({
type: "GET",
url: "usersearch.php",
data: {
input: inputvalue
},
success: function(msg) {
$('#popupwithuserssearch').html(msg);
}
});
})
After i run my query in usersearch.php I echo the html to generate a table :
$cell='1';
while (!$rs->EOF){
echo '<tr>';
echo '<td align="left" class="cell-'.$cell.'" bgcolor="#F0F0F0"><a href="#" style="color:black;" id="clicked">'.$rs->fields('name').'</a></td><br>';
$cell = $cell+1;
$rs->movenext();
echo ' </tr>';
} ';
now i'm trying to get the value if the a href is clicked i tried this already:
$("#clicked").click(function () {
var value = $(".testClick").attr("href");
alert(value );
});
I'm probably looking over it can somebody help me out
Thanks!
Well, remove your ID cause it must be a unique, and assign a class instead.
Try this code
$(".clicked").click(function () {
var value = $(this).attr("href");
alert(value);
});
To prevent a redirect by clicking on your A tag add following line event.preventDefault();
to your click function.
Since no value to href
at the moment I'm displaying the ID
// Add content dynamically $(document).ready(function(){ var content = '<table>'; for(var i = 0 ; i < 10 ; i++){ content += '<tr><td align="left" class="cell-'+i+'" bgcolor="#F0F0F0"><a href="#" style="color:black;" class="clicked" id="clicked_'+i+'">'+i+'</a></td></tr>' } content += '</table>'; $('#add-dynamic-content').append(content) }); // Click event $(document).on('click','.clicked', function(){ alert($(this).attr('id')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='add-dynamic-content'></div>
$(document).on('click', '#clicked', function () {
var value = $(".testClick").attr("href");
alert(value );
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.