Difference between revisions of "JQuery Sandbox"
From Wikicliki
Line 59: | Line 59: | ||
alert(countrycode); | alert(countrycode); | ||
}); | }); | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | == THE GOOD SHIT LOLLIPOP == | ||
+ | |||
+ | <pre> | ||
+ | <html> | ||
+ | <head> | ||
+ | <title>EXPERIMENTS</title> | ||
+ | <style> | ||
+ | .item {font-size:10px; border:5px solid black; padding:2em} | ||
+ | div.redcolor {font-size:30px} | ||
+ | </style> | ||
+ | <script type="text/javascript" src="jquery.js"></script> | ||
+ | <script type="text/javascript"> | ||
+ | |||
+ | // SINGLE LINE COMMENT | ||
+ | /* ANOTHER SINGLE COMMENT */ | ||
+ | /* | ||
+ | * MULTI LINE | ||
+ | * COMMENTS!!! | ||
+ | */ | ||
+ | |||
+ | $(document).ready(function(){ | ||
+ | // basic container for jQuery | ||
+ | } | ||
+ | </script> | ||
+ | </head> | ||
+ | <body> | ||
+ | <div class=item> | ||
+ | <script language="javascript"> | ||
+ | // LIST OF MULTIPLES | ||
+ | document.write("<h2>1</h2>"); | ||
+ | for(i = 1, fact = 1; i < 5; i++, fact *=i){ | ||
+ | document.write(i + "! = " + fact); | ||
+ | document.write("<br>"); | ||
+ | } | ||
+ | for(i = 1, multiple = 1; i < 5; i++, multiple *=i){ | ||
+ | document.write(i + "<sup>2</sup> = " + multiple); | ||
+ | document.write("<br>"); | ||
+ | } | ||
+ | </script> | ||
+ | </div> | ||
+ | <br> | ||
+ | <div class=item> | ||
+ | <script language="javascript"> | ||
+ | $(document).ready(function(){ | ||
+ | $("#NN").click(function(){ | ||
+ | var NNhttp; | ||
+ | var NNhttp = this.href; | ||
+ | alert (NNhttp); | ||
+ | }); | ||
+ | |||
+ | $("#MM").click(function(){ | ||
+ | var countryname = this.href; | ||
+ | var countrycode = countryname.substring(countryname.length-2); | ||
+ | alert(countrycode); | ||
+ | }); | ||
+ | }); | ||
+ | |||
+ | </script> | ||
+ | |||
+ | <a name="NN" id="NN" href="experiment.html?countrycode=NN"> Reports its own URL in an alert</a><br> | ||
+ | <a name="MM" id="MM" href="experiment.html?countrycode=MM"> Reports its own countrycode in alert</a><br> | ||
+ | |||
+ | </div> | ||
+ | <br> | ||
+ | <div class=item> | ||
+ | <script language="javascript"> | ||
+ | |||
+ | $("#fadingthing").click(function( | ||
+ | addClass("redcolor");) | ||
+ | { | ||
+ | event.preventDefault(); | ||
+ | $(this).hide("slow"); | ||
+ | }); | ||
+ | </script> | ||
+ | <div id=fadingthing style="width:100px;height:100px; background:lightblue">BLUE</div> | ||
+ | |||
+ | </div> | ||
+ | </body> | ||
+ | </html> | ||
</pre> | </pre> |
Revision as of 16:54, 3 September 2008
http://effectize.com/jquery-developer-guide
experiment one
this experiment prints the contents of the textarea in an alert, and also prints the tagname of the clicked items into the first span tag available....
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("a").click(function(){ var curText= $("#myText").val(); var urlname = curText; alert(urlname); }); $("*", document.body).click(function (e) { e.stopPropagation(); var getthething = $(this).get(0); $("span:first").text("Clicked on - " + getthething.tagName); }); }); </script> </head> <body> <a name="namename" id="idname" href="index2.html"> non nummy bong bong</a> <br><br> <span class=first></span> <br><br> <textarea name="myText" rows="5" cols="20" id="myText" style="width:250px;"></textarea> </body> </html>
assuming im trying to get the countrycode from a url thats like "summerisle?countrycode=SM" and you want to retrieve just the code at the end. you can use jquery along with Regular Expressions
$("a").click(function(){ var curText= $("#element"); var urlname = curText; alert(this.href.match(/countrycode=(\w\w)/)); });
alternatively you can do the same thing with substrings
$("a").click(function(){ var countryname = this.href; var countrycode = countryname.substring(countryname.length-2); alert(countrycode); });
THE GOOD SHIT LOLLIPOP
<html> <head> <title>EXPERIMENTS</title> <style> .item {font-size:10px; border:5px solid black; padding:2em} div.redcolor {font-size:30px} </style> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> // SINGLE LINE COMMENT /* ANOTHER SINGLE COMMENT */ /* * MULTI LINE * COMMENTS!!! */ $(document).ready(function(){ // basic container for jQuery } </script> </head> <body> <div class=item> <script language="javascript"> // LIST OF MULTIPLES document.write("<h2>1</h2>"); for(i = 1, fact = 1; i < 5; i++, fact *=i){ document.write(i + "! = " + fact); document.write("<br>"); } for(i = 1, multiple = 1; i < 5; i++, multiple *=i){ document.write(i + "<sup>2</sup> = " + multiple); document.write("<br>"); } </script> </div> <br> <div class=item> <script language="javascript"> $(document).ready(function(){ $("#NN").click(function(){ var NNhttp; var NNhttp = this.href; alert (NNhttp); }); $("#MM").click(function(){ var countryname = this.href; var countrycode = countryname.substring(countryname.length-2); alert(countrycode); }); }); </script> <a name="NN" id="NN" href="experiment.html?countrycode=NN"> Reports its own URL in an alert</a><br> <a name="MM" id="MM" href="experiment.html?countrycode=MM"> Reports its own countrycode in alert</a><br> </div> <br> <div class=item> <script language="javascript"> $("#fadingthing").click(function( addClass("redcolor");) { event.preventDefault(); $(this).hide("slow"); }); </script> <div id=fadingthing style="width:100px;height:100px; background:lightblue">BLUE</div> </div> </body> </html>