3

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.

2
  • Can you please post your code in details? Commented Aug 18, 2011 at 12:29
  • Use the button marked as {} to add 4 spaces to a line to display it as code. Commented Aug 18, 2011 at 12:33

4 Answers 4

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.

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

2 Comments

sorry i know this solution.. but i want to pass the value directly in function.
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.
3
<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'));"/>

Comments

2

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/

1 Comment

@Tanu, which browser and version are you using?
1

In javascript function you can access textbox value like this

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

1 Comment

Well, it is not getElementsByName. Instead it should be : var text=document.getElementById("textbox1").value;

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.