0

I am working on an app where the backend is written in RoR and the front end is written completely in javascript/jquery/ajax. My question is how can I pass a variable into a url for my ajax get request? My code:

    $(document).ready(function() {

        $("button").click(function() {


            $.getJSON('http://www.mydomain.com/recipes/1', function(results) {
            $('#1').val(results.recipe_name)
            $('#2').val(results.author)
            $('#3').val(results.level_of_difficulty)
            $('#4').val(results.instructions)
            $('#5').val(results.activity)


            })

        })

    })

Right now, the URL is in the format /recipes/:id and I want the :id to be a variable so that the data will load correctly when I click "show" for different recipes... so something like http://www.mydomain/recipies/recipe_x

Thanks in advance!

1 Answer 1

2

You can set the id of the recipe on each link using the data attribute. And then use the id to change the URL.

<a data-id="1">Recipe 1</a>

$("button").click(function(this) {

$.getJSON('http://www.mydomain.com/recipes/' + $(this).data("id")

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

1 Comment

It will require jquery.

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.