0

I have an ajax navigation system in my app. Each ajax link is of class ajax and the page it needs to load is saved in an aurl attribute, for example:

<a class="ajax" aurl="/user/posts/3/edit">Edit Post</a>

The aurl is given to the element using symfony's url_for method in the server-side.

I thought about more elegant way of saving the element's aurl attribute, like jQuery.data(), but how can i use it from the server side? creating a script after each element to set it's aurl doesn't seem like a good solution..

Any ideas?

1 Answer 1

3
<a class="ajax" data-aurl="/user/posts/3/edit">Edit Post</a>

Then you can access it through .data('aurl') if you use a recent jQuery version (1.5 or newer AFAIK).

However, why don't you simply set the href (which should be set anyway) and then use $(this).attr('href') to get the url and use e.preventDefault(); (with e being the first argument of the click handler function) to prevent the link from loading without AJAX?

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

2 Comments

I wanted to use data so that i don't have to spread the links all over the app.. so I would have <a class="ajax edit">Edit Post</a> and it's url will be saved "behind the scenes"
If you want to set data related to the element it should be set there, i.e. through a data- attribute. Moving URLs away from the related links would just make things confusing.

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.