Difference between revisions of "JQuery Sandbox"
From Wikicliki
Line 38: | Line 38: | ||
</pre> | </pre> | ||
− | + | 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]] | |
+ | |||
<pre> | <pre> | ||
$("a").click(function(){ | $("a").click(function(){ | ||
Line 45: | Line 46: | ||
alert(this.href.match(/countrycode=(\w\w)/)); | alert(this.href.match(/countrycode=(\w\w)/)); | ||
}); | }); | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | alternatively you can do the same thing with substrings | ||
+ | |||
<pre> | <pre> | ||
+ | $("a").click(function(){ | ||
+ | var countryname = this.href; | ||
+ | var countrycode = countryname.substring(countryname.length-2); | ||
+ | alert(countrycode); | ||
+ | }); | ||
+ | </pre> |
Revision as of 12:58, 22 August 2008
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); });