Welcome to WebmasterWorld Guest from 54.166.152.121

Forum Moderators: open

Message Too Old, No Replies

jquery - accesing link variables

     
8:13 pm on Oct 30, 2008 (gmt 0)

10+ Year Member



I'm a beginner when it comes to jquery, spend most of my time with php/mysql and simple javascript. I've been searching high and low for a demo on how to access a variable in a url when calling jquery's post method. Basically I have a whole pile of links that have dates attached to them, ie.:

<a href"domain.com?var=20081031">

Using jquery's post method I need to send that date to an external file and then do some fun stuff with it server side. I know how to structure the function with the post method by I don't know how to pass the date as a parameter, ie. how to access it.

All the tutorials I've seen use forms so they have onclick events which just grab the id of the element, which doesn't help me for a few reasons. (Elegant deprecation for onclick, and multiple variables.) I would even be fine with passing the entire url and parsing it server side. I just need to post that date!

Does anyone familiar with jquery have an idea?

Cheers!

11:08 am on Oct 31, 2008 (gmt 0)

5+ Year Member



hello,
could you change the link slightly so that you put the date variable that you need into an attribute?
EG.
<a href="#" class="dateLink" dateVar="20081031">Click me baby!</a>

Then in your JS you can access / post the variable like so:

$(".dateLink").click(function(){
$.post("myscript.php", {
dateVar: $(this).attr('dateVar')
},
function(xml){
//do something here onn call back
});
});

hope this helps

Ally

4:48 pm on Oct 31, 2008 (gmt 0)

10+ Year Member



Good deal. The following works perfect:

jQuery(document).ready(function(){
$('.calendarLink').click(function(ev)
{
var prefix = 'date_';
var date = this.id.substring( ( prefix.length ) );

$.ajax({
type: "POST",
url: "script.php",
data: "date="+date,
success: function(msg){
$('#result').html(msg);
}
});
});
});

<a href="javascript:void(0);" class="calendarLink" id="date_20081010">October 10,2008</a>
<div id="result"></div>

Thanks!

 

Featured Threads

Hot Threads This Week

Hot Threads This Month