Have a interesting problem. I make a little ajax mechanism to get info from php file and add a function for delete the data.
phpfile.php:

<?php
if(isset($_POST['bu'])){
$data = 'text<a onclick="something();" href="#">alert</a>';
echo $data;
}
?>

js file:

function getdatafromfile(){
var bu = 'bu';
$.ajax({
			type: 'POST',
			url: 'phpfile.php',
			data: {bu: bu},
			success: function(msg){ 
			$("#msg").html(msg);
			getContent();
			}
		});
}

function something(){
alert("alert");
}

Data from the php file is fine, but when I click on the link (<a onclick="something();" href="#">alert</a>) getted from ajax, function don't work.
How to fix that?
Thanks in advance!

That approach can be made to work but it is generally considered better to attach event handlers in javascript rather than HTML.

<?php
if(isset($_POST['bu'])){
  $data = 'text <a href="#">alert</a>';
  echo $data;
}
?>
function getdatafromfile(){
  var bu = 'bu';
  $.ajax({
    type: 'POST',
    url: 'phpfile.php',
    data: {bu: bu},
    success: function(msg){ 
      $("#msg").html(msg).find('a').click(function(){
        something();
        return false;//which ever way you attach it, be sure to return false from the handler to suppress the <a> tag's default hyperlink action.
      });
    }
  });
}

Airshow

That is my mistake, but the script still does not work. And I apply handlers in the html, because my php script is a get data from my database and I try to make unique delete button for each row(item). Thank you. I'm really not good js writer, but I like the idea to get,change,delete data from database without refresh the page and everything went well until I came here ( to this problem)

What you are trying to write is generally called a "Create, Read, Update, Delete" (CRUD) interface.

There's no reason why handlers for all the buttons should not be attached in javascript.

Try a web search and you should find some sample scripts.

Airshow

Thanks for the help, solution of the problem is stop using jQuery and come back to old school ajax & javascript. I just don't understand the idea of jQuery. He is great library, but not for me.

Personally, I wouldn't attempt this sort of thing without jQuery. Raw javascript would take longer to write and would be far more volumous (though it might actually be more efficient).

jQuery takes some getting used to but is very rewarding once you have made the investment.

The best jQuery programmers are the ones who were good javascript programmers first, so yes carry on with your raw js approach with a view to translating your code to jQuery at some point in the future when time permits.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.