6

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?

4 Answers 4

10

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>

<script type="text/javascript">
    $('a.picLinks').click(function () {
        //split at the '_' and take the second offset
        var picNumber = $(this).attr('id').split('_')[1];
        viewer.show(picNumber);
    });
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

where does picNumberLink come from since this is a variable?
@me - does that make more sense now?
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
@me - then see my answer. If you give your picture links the class="picLinks" that will do it.
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
|
2
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
}

4 Comments

my question is around how i can have the javascript pic up the picNumberLink variable now that its not inline
Add it to hidden field next to link, then - in functionIWantToCall use "var picNumberLink = wrappedLink.next().val()"
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
<input type='hidden' value='<%= Model.PictureUrl %>' />
1
$(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 }) %>

2 Comments

$(document).ready(function(){}) can be abreviated to $(function(){})
for this to work, would i need the following <a href="" picNumber+'3'><img src=".."></a>
1

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]);
  });
}}

1 Comment

Just realised this is very much like karim79's answer... I think we need to see me's HTML

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.