2

Simple question. How do I pass a string to a javascript function and make a popup box alert containing the contents of that string? Here's what I have:

The test.html.erb file.

<%= link_to 'Test','#', onclick: 'test(helloworld)', class: "btn btn-xs btn-primary" %>

Here's the test.js file:

function test(name){
     alert(name);
}

However, when I click on the "Test" link that gets generated, "helloworld" doesn't pop up. Nothing happens.

2 Answers 2

5
onclick: 'test(helloworld)' 

would pass the variable named helloworld to the function. Instead, you want to pass a string literal, like:

onclick: 'test("helloworld")'
Sign up to request clarification or add additional context in comments.

Comments

2

Wrap helloworld in quotes when you pass it in:

onclick: 'test("helloworld")'

I imagine there is some kind of error in the console as well.

1 Comment

Perfect! Thanks. I knew it was something simple.

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.