MikeD wrote:Chris wrote:
using jQuery (download from
http://jquery.com/):
http://api.jquery.com/jQuery.get/
Code: Select all
<!DOCTYPE html>
<html>
<head>
<title>Hello!</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"> // inculde jquery </script>
<script type="text/javascript">
$(document).ready( function()
{
// add a click event to our link
$('a[href="#"]').click( function()
{
$.get('myFile.html', function(data)
{
$('#myDiv').html(data); // fill our div with the data
});
})
});
</script>
</head>
<body>
<a href="#">Click here and watch the magic happen</a>
<div id="myDiv" style="border: #000 1px solid;">this div will contain the response :D</div>
</body>
</html>
Is the only way to do this by using the '#' symbol? I can't get it to work with anything else other than that, even when I do change the $('a[href="#"]').click( function() to whatever symbol.
No, that was more for the example, I like to add these things so people ask questions. So well done to you.
Basically jQuery makes use of CSS like selectors.
http://www.w3.org/TR/selectors/#selectors
You might notice in the HTML I have an anchor with the href attribute set to #.
Code: Select all
<a href="#">Click here and watch the magic happen</a>
Basically $('a[href="#"]') will search for all a tags with the attribute href set to #, I could search for anything I want though.
Code: Select all
$('a[href="lol.php"]').click( function() { alert('haha') } );
Code: Select all
$('a[id="myLink"]').click( function() { alert('haha') } );
Code: Select all
<a href="lol.php" id="myLink">lol</a>
or simply:
Code: Select all
$('#myLink').click( function() { alert('haha') } );
You can also just simply search all anchor elements:
Code: Select all
$('a').click( function(){ alert($(this).attr('href')) });
Code: Select all
<a href="haha">lol</a><br />
<a href="pff">:D</a>
This can of course also be used for different types of elements:
Code: Select all
$('input').click( function() { alert('you clicked the button') } );
Code: Select all
<input type="button" value="click me" />
Fighting for peace is declaring war on war. If you want peace be peaceful.