admin管理员组

文章数量:1393920

In my jsp page, I have one button and one label. When page loading, the text of the label box is "Test". Suppose, user clicks the button then I have to change the text of the label into "tested".

I am new to Jsp.So, Please guide me to get out of this issue...

In my jsp page, I have one button and one label. When page loading, the text of the label box is "Test". Suppose, user clicks the button then I have to change the text of the label into "tested".

I am new to Jsp.So, Please guide me to get out of this issue...

Share Improve this question edited Sep 27, 2019 at 22:13 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 17, 2012 at 5:49 SaravananSaravanan 11.6k46 gold badges147 silver badges213 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You would first need to set a listener to your button, say our markup was as follows:

<button id='btn'>test it</button>
<span id='label'>Test</span>

We can select the button element, and set it's onclick attribute:

document.getElementById('btn').onclick = test

Then we can define a function call test that will toggle the label:

function test(){
 document.getElementById('label').innerHTML = 'Tested';
}

And that's it!

Here's a jsfiddle demonstrating this code.

    <script type="text/javascript">
function fnc()
{
    document.getElementById("atext").innerHTML="tested";
}
</script>

<label id="atext">test</label>
<input type="button" onClick="fnc()" value="click me">

本文标签: javascriptHow to change the text of the label box dynamically in jsp pageStack Overflow