最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to pass string in onclick function html - Stack Overflow

programmeradmin3浏览0评论

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>

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>

Share Improve this question edited Aug 25, 2018 at 18:21 O'Neil 3,8494 gold badges17 silver badges32 bronze badges asked Mar 4, 2016 at 9:56 user5331648user5331648 4
  • 7 <a href="#" onclick="test('TEST123')"> or JS parser will look for variable having name as TEST123 – Rayon Commented Mar 4, 2016 at 9:56
  • 1 <a href="#" onclick="test('TEST123')"> – Niklesh Raut Commented Mar 4, 2016 at 9:56
  • <a href="#" onclick="test('TEST123')"> – Apb Commented Mar 4, 2016 at 9:57
  • I agree with @LearningMode and am upvoting his answer. – Arif Burhan Commented Mar 4, 2016 at 9:59
Add a ment  | 

3 Answers 3

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

just wite string parameter in your js function

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

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.

发布评论

评论列表(0)

  1. 暂无评论