I’m trying to load div content based on the a href being clicked and pass in a parameter. For example, Link 1
Clicking on Link 1 will pass value “3” to process.php, and return back the value “apple is good for you”.
However, I can’t seem to be able to pass the value without having a submit button. Anyway I can pass the parameter to another php file to process, and return the value?
$(document).ready(function(){
$("#testing a").live("click", function(evt){
var id= $(this).attr('id');
$.post("process.php", { id: id },
function(data) {
alert(data);
$('#result').load(data);
});
})
});
Below is my HTML
<div id="testing">
<a href="" id="11"> hello </a>
</div>
<div id="result"></div>
Appreciate your help, thanks a lot!
You shouldn’t be using numbers for
idvalues. Instead, prefix them with a letter, or consider adding them to adata-atribute on the element.Additionally,
$.live()is deprecated, and we are encouraged to use$.on()from here on out for event delegation. I’ve gone ahead and handled this in the code below, but theidissue remains.Lastly,
$.load()and$.html()aren’t the same. If you want to loaddatainto an element, you don’t call the load method (though the name could lead to that confusion).From your
process.phpfile, you might have something like the following: