Hey guys,
To get value from input text field, it involves 4 steps:
1.Add id or class attribute to the input text field you want to access.
2.Add a Button tag and add click event listener to display value.
3.Add a Paragraph tag,to display that value on button click.
<input type="text" id="name" placeholder="Name" />
<button onclick="function_to_show()">Show Value</button>
<p id="display_here"></p>
4.Now,write the code to get and display that value using javascript function.
<script>
var name1;
function function_to_show()
{
name1=document.getElementById("name").value; document.getElementById("display_here").textContent=name1;
}
</script>
Explanation:
In javascript, we declared a variable 'name1', where we will store value of input text field. After that we defined a function, in that function, we wrote DOM object model related to id, in that model, we kept the input field id name "name". Store that value in a variable, which is declared above. Then we have to display that value in Paragraph tag.To do that,again use DOM object model, and give the id of the element where you want to display. In this one, we are giving "display_here" id. So, value will be displayed in that place.
So, in the browser, when you click on "Show Value" button, the above function is executed and displays the value in given element.
Hope this finds helpful, Please do comment, if you have any doubts or facing problems in javascript or html, css, php, mysql, will do a post on it.
Thanks!
Comments
Post a Comment
If you have any doubts, Please do comment