0

This isnt working, not sure what is wrong. I dont want to use href onclick at all, i usually use an id on ahref links then execure javascript that way but i need to pass parameters from the link to the function, not sure if there are other alternatives?

code using the parameters isnt shown, its basiacly a forum link but its loading the topic into a div

function changetotopicdetails(topicid, topicname) {
    $('#loadingAjaxs').show();
    $('#flubestext').hide();

<a href="javascript:changetotopicdetails(@following.Id, @following.ShortName);">@following.Title</a>

I would usually do something like

$('#changeuserstwohour').click(function () {
       $('#userswrap').load('@Url.Action("TrendingUsersMenutwohr", "Trending")');
    });

but by doing so i cant send parameters to the function during a loop (list of topics)

any suggestions?

Answers pointed out that i need the variables passed to a new {}

$('#changeuserstwohour').click(function () {
       $('#userswrap').load('@Url.Action("TrendingUsersMenutwohr", "Trending", new {@theid = id, @thename = name})');
    });

2 Answers 2

1

You could do your usual click function, eventListener or bind and use data values to indicate you username and trending values.

so your tag would look like

<a href="#" data-id='@following.Id' data-short-name='@following.ShortName'>@following.Title</a>

and then your usual click function which would look like

$('#changeuserstwohour').click(function () {
   $('#userswrap').load('@Url.Action($(this).data(id), $(this).data(short-name)');
});

you may need to use .each() if you have multiples or call it after each ajax load to make sure its listening to the new objects. I haven't tested this as I dont really have the functions but this should work! Please let me know how it goes and if you have another question :)

Sign up to request clarification or add additional context in comments.

4 Comments

hey i dont think this works for what I need, its great tho as didnt know this was possible. but the formatting of this may make it not work?
getting unexpencted character $ $('#contentwrap').load('@Url.Action("Detail", "Topics", new {@id = $(this).data(id), @name = $(this).data(short-name)})', function () { $('#loadingAjaxs').hide(); $('#flubestext').show(); window.history.pushState(null, 'title', '/users'); })
Hi, I think you may need to get rid of the first $? ;)
You need to look at what you're doing here - you're mixing jquery syntax into razor markup. All of the razor code runs before the HTML is sent to the browser, so you cannot use a Url.Action helper and pass it jquery method results. For what you're doing in that comment, you'd simply call write $('#contentwrap').load('@Url.Action("Detail","Topics", new { id=CSharp_Variable_With_Id, name=CSharp_Variable_WithName})', function(){callbackStuffHere();}
1

A combination of both techniques provided so far is what you're probably really after.

Your markup for your individual action links would look like:

 <a href="#" class="selectorForYourActions" data-id='@following.Id' data-short-name='@following.ShortName'>@following.Title</a>

and then your on-click callback which would look like

$('#idOfContainerYourLoopCreatesItemsWithin').on("click", "a.selectorForYourActions", function () {
   $('.userswrap').load('@Url.Action($(this).data(id), $(this).data(short-name)');
});

This will catch any dynamically created items that match that class if they are created within the element selected by the ID selector, but only handle clicks on anchor tags with the marker class, so you can both have many of those anchors with unique (or no) Ids and anchors with normal (or different) functionality.

6 Comments

Thanks for copying my answer?
I started by doing it as edits to your answer, then realized they were more than grammar alterations. You'll note that it's presenting the case using jquery.on, event bubbling, and limiting. That's more than an 'edit' worth of difference, IMO. If you prefer I'll just gut your answer to replace it with my version as an edit....
Ok, no worries then, theres no need to 'gut' my answer.
"Gut" might be overly strong a term, but it was going to involve a lot of edits that weren't the sorts of improvements usually covered by the usage of 'edit' was my point.
That's okay, I'm sorry I jumped to conclusions. :)
|

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.