views:

67

answers:

2

Hey ho,

this is probably a simple question, but I did not find an answer so far.

Given I have:

$("a.clickable").livequery('click', (function (e) {
         var values = $(this).attr('id').split('_');
         $('#' + values[3]).load($(this).attr('href'));
         e.preventDefault();
     }));

You see, I want all links with the class "clickable" to cause an ajax load. The target div however varies, so I´ve simply put this - along with some other data - into the Id´s link: "link_value1_value2_targetDivId".

So I currently split the id to get an array of values to work with. I know about $(obj).data(), but I need to pass some stuff on serverside.

Is there a ways to achieve this without this ugly id-string-parsing? ASP.Net/MVC btw.

Thanks for any suggestion

A: 

No You can't do that. All that triggering of the AJAX calls has to be done from the client side, though there are ways where you can push some data to client from the server using reverse AJAX or comet but that is not what you are looking for. For your case, you have to do that ugly logic. But if you don't want to parse it, you can store the id in the element itself as data/custom attribute and use it instead of parsing.

Teja Kantamneni
Custom attributes was the keyword that helped me. I found some nice info about this, which in my case is preferable over data(). Thanks
Steffen
A: 

I hope I understand the question - there are several and each is related to its div. After the is clicked the related div is manipulated.

How I would do that. On server side (in aspx file) I would generate javascript code like this:

var mapAIdToDivId = new Array();
<% foreach(Item i in MyDivAndACollection) { %>
  mapAIdToDivId['<%= i.AId %>'] = <%= i.DivId %>;
<% } %>

Then the js handler would be

$("a.clickable").livequery('click', (function (e) {
   $('#' + mapAIdToDivId[$(this).attr('id')]).load($(this).attr('href'));
   e.preventDefault();
}));
stej
Thank you for your reply. This ia an approach I already tried, but was not pleased with
Steffen