admin管理员组文章数量:1405535
i need a javascript function to be called on certain button click, upon that click the button name will change. ie,
<input type=button id = button1 value=boy onclick=function(boy,girl)>
i need the javascript function to take 2 parameters and check if the value of the button is the first paramter, then the value will bee the 2nd and vice versa.
so if i press the button and it says BOY, it will bee Girl and if i press the button and it says Girl it will bee boy! using javascript please thx.
i need a javascript function to be called on certain button click, upon that click the button name will change. ie,
<input type=button id = button1 value=boy onclick=function(boy,girl)>
i need the javascript function to take 2 parameters and check if the value of the button is the first paramter, then the value will bee the 2nd and vice versa.
so if i press the button and it says BOY, it will bee Girl and if i press the button and it says Girl it will bee boy! using javascript please thx.
Share asked Sep 21, 2011 at 10:33 GhassanGhassan 3515 gold badges10 silver badges19 bronze badges5 Answers
Reset to default 3var foo = function(a,b,c){
if(a.value==b){
a.value=c;
}
else{
a.value=b;
}
};
and for the button
<input type="button" id="button1" value="boy" onclick="foo(this,"boy","girl")">
see here: http://jsfiddle/w9ed8/
or:
<input type=button id = button1 value=boy onclick="changeMe(this)">
<script>
function changeMe(obj){
if(obj.value == "boy"){
obj.value = "girl"
}else{
obj.value = "boy"
}
}
</script>
Or
<input type=button id = button1 value=boy onclick="changeMe(this, 'boy' , 'girl')">
<script>
function changeMe(obj , param1 , param2){
if(obj.value == param1 ){
obj.value = param2
}else{
obj.value = param1
}
}
</script>
There are many ways to do this, but by getById
function showed below you will have a portable function to access to DOM elements by id
:
<html>
<script type="text/javascript">
function getById(a) {
if (document.getElementById && document.getElementById(a)) {
return document.getElementById(a)
} else {
if (document.all && document.all(a)) {
return document.all(a)
} else {
if (document.layers && document.layers[a]) {
return document.layers[a]
} else {
return false
}
}
}
}
function myfunc()
{
getById("button1").value =(getById("button1").value=="boy")?"girl":"boy";
}
</script>
<head>
</head>
<input type="button" value="boy" id="button1" onclick="myfunc();" />
</html>
<input type=button id=button1 value=boy onclick='test(this,boy,girl)'>
<script>
function test(Sender,boy,girl){
Sender.value = Sender.value == boy ? girl : boy;
}
</script>
function change()
{
if((document.getElementById("button1").value)=="Boy")
{
document.getElementById("button1").value="Girl"
}
else
{
document.getElementById("button1").value="Boy"
}
}
本文标签: htmlJavascript to change button textStack Overflow
版权声明:本文标题:html - Javascript to change button text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744911184a2631927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论