Inline onclick JavaScript variable

后端 未结 3 578
抹茶落季
抹茶落季 2020-12-05 11:33

I have a JavaScript above this html is there any way to pass it inside EditBanner(JS Variable Here) in code below ?

//EditBanner to be changed to pass a Js v         


        
相关标签:
3条回答
  • 2020-12-05 12:09

    Yes, JavaScript variables will exist in the scope they are created.

    var bannerID = 55;
    
    <input id="EditBanner" type="button" 
      value="Edit Image" onclick="EditBanner(bannerID);"/>
    
    
    function EditBanner(id) {
       //Do something with id
    }
    

    If you use event handlers and jQuery it is simple also

    $("#EditBanner").click(function() {
       EditBanner(bannerID);
    });
    
    0 讨论(0)
  • 2020-12-05 12:10

    There's an entire practice that says it's a bad idea to have inline functions/styles. Taking into account you already have an ID for your button, consider

    JS

    var myvar=15;
    function init(){
        document.getElementById('EditBanner').onclick=function(){EditBanner(myvar);};
    }
    window.onload=init;
    

    HTML

    <input id="EditBanner" type="button"  value="Edit Image" />
    
    0 讨论(0)
  • 2020-12-05 12:10
    <script>var myVar = 15;</script>
    <input id="EditBanner" type="button" value="Edit Image" onclick="EditBanner(myVar);"/>
    
    0 讨论(0)
提交回复
热议问题