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

How To pass Element Id In Javascript As Argument or parameter - Stack Overflow

programmeradmin4浏览0评论

how it is possible to get element from the id of input element and pass it as parameter to java script function.

 <html>
 <body>
 <input type="text" id="name">
 <input type="button" onclick="call(id_of_input_type_text)" value="Click 
  me">
 <script>
 var call(id_of_input_type_text) = function(){
 var x = document.getElementById(id_of_input_type_text).value;
 alert(x);
 }
 </script>
 </body>
</html>

Sir/Mam I want to use single function like validation and get there value by pass id in the function so please help me regarding this problem

how it is possible to get element from the id of input element and pass it as parameter to java script function.

 <html>
 <body>
 <input type="text" id="name">
 <input type="button" onclick="call(id_of_input_type_text)" value="Click 
  me">
 <script>
 var call(id_of_input_type_text) = function(){
 var x = document.getElementById(id_of_input_type_text).value;
 alert(x);
 }
 </script>
 </body>
</html>

Sir/Mam I want to use single function like validation and get there value by pass id in the function so please help me regarding this problem

Share Improve this question edited May 26, 2017 at 6:36 LF-DevJourney 28.5k30 gold badges166 silver badges316 bronze badges asked May 26, 2017 at 6:32 Shaurya SrivastavaShaurya Srivastava 1331 gold badge1 silver badge8 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 8

Option 1 (from your question):

Note you can use call('name') in this case.

var call = function(id){
  var x = document.getElementById(id).value;
  alert(x);
}
<input type="text" id="name">
<input type="button" onclick="call(document.getElementById('name').id)" value="Click me">

Option 2 (send the element, so you won't need to get it in the function):

var call = function(elem){
  var x = elem.value;
  alert(x);
}
<input type="text" id="name">
<input type="button" onclick="call(document.getElementById('name'))" value="Click me">

Use the same function with different arguments for each call. Like you can use:

<input type="button" onclick="call('name')" value="Click Me">

And it will alert the value of input field with id 'name'.

Hope this helps.

You can use the below code as reference:

<body>
 <input type="text" id="name">
 <input type="button" onClick="call('name')" value="Click me" id="btnOne">

 
 <script type="text/javascript">
var call = function(elementId)
{
 var valueOfInput = document.getElementById(elementId).value
    alert(valueOfInput);
}
</script>
 </body>

发布评论

评论列表(0)

  1. 暂无评论