views:

1718

answers:

4

i am converting over from websforms to asp.net mvc and i have a question.

i have a loop that generates links dynamicallly where picNumberLink is a variable in a loop and image is a variable image link.

i want to avoid putting javascript actions inline so in my webforms project is did the following:

hyperLink.Attributes.Add("onclick", "javascript:void(viewer.show(" + picNumberlink + "))");

what is the equivalent using jquery in asp.net mvc?

I have seen examples of using the $(document).ready event to attach on clicks but i can't figure out the syntax to pass in the picNumberLink variable into the javascript function.

suggestions?

+1  A: 
var functionIWantToCall = function(){
    var wrappedLink = $(this);
    //some serious action
}

//this should be called on document ready
$("#myLinkId").click(functionIWantToCall);

If you need to get URL of picture, keep it in anchor`s href:

var functionIWantToCall = function(event){
    event.preventDefault(); //this one is important
    var link = $(this).attr('href');
    //some serious action
}
Arnis L.
my question is around how i can have the javascript pic up the picNumberLink variable now that its not inline
ooo
Add it to hidden field next to link, then - in functionIWantToCall use "var picNumberLink = wrappedLink.next().val()"
Arnis L.
that seem like a kluge but if there is no more elegant way then thanks for the idea. what would be the syntax of it, just a regular hidden html tag
ooo
Check out edit of my post for another approach.
Arnis L.
<input type='hidden' value='<%= Model.PictureUrl %>' />
Arnis L.
+4  A: 

EDIT: If you generate your links with the ID of this form:

<a id="piclink_1" class="picLinks">...</a>
<a id="picLink_2" class="picLinks">...</a>

$('a.picLinks').click(function() {
    //split at the '_' and take the second offset
    var picNumber = $(this).attr('id').split('_')[1]; 
    viewer.show(picNumber);
});
karim79
where does picNumberLink come from since this is a variable?
ooo
@me - does that make more sense now?
karim79
maybe that is my issue / question. I just have an image so where can i shove the picNumber into so i would be able to grab it from javascript? i am not displaying the text
ooo
@me - then see my answer. If you give your picture links the class="picLinks" that will do it.
karim79
but i am not displaying any text in my link . . just the image.. if the number was in the text it would be perfect but its not
ooo
@me - Then extract it from the ID, I'll edit the answer now.
karim79
@me - please see my edit.
karim79
this edit look correct to me but when i click the javascript doesn't seem to fire:my link code is now:<a id="picLinks_1" class="picLinks" href=''><img src=".." /></a>and my javascript is copied exactly from above. any thoughts?
ooo
thanks for your effort . . i just figured out the latest issue and fixed it.. my jquery.js link had a typo in it :(. all is good now
ooo
A: 
$(document).ready(function()
{

$('#LinkID').click(function() {
    viewer.show($(this).attr('picNumber'));
});

});

You can add an attribute called picNumber to your hyperlink tag and set this is your mvc view

The link in your view might look something like this:

<%= Html.ActionLink("Index", new { id = "LINKID", picNumber = 1 }) %>
Rigobert Song
$(document).ready(function(){}) can be abreviated to $(function(){})
David Kemp
for this to work, would i need the following<a href="" picNumber+'3'><img src=".."></a>
ooo
<a href="#" picNumber=3>...
Rigobert Song
+1  A: 

Assuming you're able to change the HTML you output, can't you put the picNumberLink in the id or class attribute?

HTML:

<a href="..." id="piclink-242" class="view foo-242"><img src="..."/></a>

jQuery:

$(function() {
  // using the id attribute:
  $('.view').click(function() {
    viewer.show(+/-(\d+)/.exec(this.id)[1]);
  });

  // or using the class attribute:
  $('.view').click(function() {
    viewer.show(+/(^|\s)foo-(\d+)(\s|$)/.exec(this.className)[2]);
  });
}}
searlea
Just realised this is very much like karim79's answer... I think we need to see *me*'s HTML
searlea