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

How to pass html textbox value to a javascript function - Stack Overflow

programmeradmin0浏览0评论

I have a HTML textbox where user will input some string which i want to pass to a JavaScript function:

<input type="text" id="ad_search_query" style="width:295px">&nbsp;
<input type="button" value="<s:text name="button.search"/>" class="p-userButton"
 onClick="ADSEARCHGF.showTable('');"/> 

Please suggest how can i do it.

I have a HTML textbox where user will input some string which i want to pass to a JavaScript function:

<input type="text" id="ad_search_query" style="width:295px">&nbsp;
<input type="button" value="<s:text name="button.search"/>" class="p-userButton"
 onClick="ADSEARCHGF.showTable('');"/> 

Please suggest how can i do it.

Share Improve this question edited Dec 11, 2012 at 7:11 M4N 96.6k45 gold badges224 silver badges264 bronze badges asked Aug 18, 2011 at 12:27 TanuTanu 311 gold badge1 silver badge3 bronze badges 2
  • Can you please post your code in details? – Arun Rana Commented Aug 18, 2011 at 12:29
  • Use the button marked as {} to add 4 spaces to a line to display it as code. – StuperUser Commented Aug 18, 2011 at 12:33
Add a comment  | 

4 Answers 4

Reset to default 5

If the event is raised by the button, you will not have the text input object to pass to the function without getting it first, getting it by id is the best way.

You can use the id of the text box:

var searchValue = document.getElementById('ad_search_query').value;

in your function, or use Ahsan Rathod's method to use this code as parameter.

<script>
function showTable(obj){
alert(obj.value)
}
</script>

<input type="text" id="ad_search_query" style="width:295px">&nbsp; 
<input type="button" value="search" class="p-userButton" onClick="showTable(document.getElementById('ad_search_query'));"/>

Do this:

JS:

<script>function showTable(val) {alert(val);} </script>

HTML:

<input type="text" id="ad_search_query" style="width:295px">&nbsp; 
<input type="button" value="search" class="p-userButton" onClick="showTable(document.getElementById('ad_search_query').value);"/>

See this Demo: http://jsfiddle.net/rathoreahsan/8ddbD/

In javascript function you can access textbox value like this

var text = document.getElementsByName("textbox1").value;

发布评论

评论列表(0)

  1. 暂无评论