3

I tried with old question, there they passing the value in js to js.But I would like to pass string from html to javascript. but it gives

Uncaught reference error

my html is

<a href="#" onclick="test(TEST123)">

and my javascript is,

function test(p){
   alert(p)
}

how can I pass the string>

4
  • 7
    <a href="#" onclick="test('TEST123')"> or JS parser will look for variable having name as TEST123 Commented Mar 4, 2016 at 9:56
  • 1
    <a href="#" onclick="test('TEST123')"> Commented Mar 4, 2016 at 9:56
  • <a href="#" onclick="test('TEST123')"> Commented Mar 4, 2016 at 9:57
  • I agree with @LearningMode and am upvoting his answer. Commented Mar 4, 2016 at 9:59

3 Answers 3

6
<a href="#" onclick="test('TEST123')">

just wite string parameter in your js function

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

Comments

4

If you write test(TEST123) then it looks for the variable with TEST123 name.
so you have to pass it as a string not as a variable

function test(p){
   alert(p)
}
<a href="#" onclick="test('TEST123')"> and

Comments

0

If you are using select tag, and want to pass the value on selecting the option item, then you can use

<select class="form-control" onchange="checkifclick('hello')" name="product_name">

and in javascript,

<script>
    function checkifclick(test) {
        alert(test);
    }

Basically, i had faced this problem once, i wanted to call the function and wanted to pass the variables into that javascript function.

class="form-control" is using to beautify the element, form-control is a build-in class of bootstrap.

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

Your Answer

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