In here, I would wanna change the value of the input box. I would wanna change the value when user clicks on the Save
button.
For example, the default value of the input box is False
but when user clicks on the save button, the value in the input box changes to True
.
Need help in solving the condition logic.
function change()
{
var change = document.getElementById("check");
if (change.value == "false")
{
document.test.savereport = "True";
document.test.submit();
}
else
{
change.value = "false";
}
}
<form name="test" method="post">
<input type="text" name="savereport" value="False" />
</form>
<div align="center">
<button type="button" value="false" id="check" onclick="change()" />Save</button>
</div>
In here, I would wanna change the value of the input box. I would wanna change the value when user clicks on the Save
button.
For example, the default value of the input box is False
but when user clicks on the save button, the value in the input box changes to True
.
Need help in solving the condition logic.
function change()
{
var change = document.getElementById("check");
if (change.value == "false")
{
document.test.savereport = "True";
document.test.submit();
}
else
{
change.value = "false";
}
}
<form name="test" method="post">
<input type="text" name="savereport" value="False" />
</form>
<div align="center">
<button type="button" value="false" id="check" onclick="change()" />Save</button>
</div>
Share
Improve this question
edited Nov 10, 2017 at 6:19
xerxes39
asked Nov 10, 2017 at 6:12
xerxes39xerxes39
451 gold badge2 silver badges7 bronze badges
1
-
Why do you check the
.value
of the clicked<button>
element? Note,<button>
element is not self-closing – guest271314 Commented Nov 10, 2017 at 6:17
4 Answers
Reset to default 0Apart from removing self-closing of button tag, replace
document.test.savereport = "True";
with
document.test.elements[ "savereport" ].value = "true"
Demo
function change() {
var change = document.getElementById("check");
if (change.value == "false") {
document.test.elements["savereport"].value = "True";
//document.test.submit();
} else {
change.value = "true";
}
}
<form name="test" method="post">
<input type="text" name="savereport" value="False" />
</form>
<div align="center">
<button type="button" value="false" id="check" onclick="change()"> Save </button>
</div>
I'm rusty with the exact syntax but the following code should do
function change()
{
var change = document.getElementById("check");
if (change.value == "1")
{
change.value = false
document.test.savereport = "True";
document.test.submit();
}
else
{
change.value = true
}
}
I don't understand why you need to go through all the trouble of reading values for this particular requirement.
You are reading the value, paring it to False
, leaving it as is if so or if True
, you change it to False
.
Hence essentially, you are just leaving the value to be False
no matter what the earlier value was.
So, simply change the value in savereport
upon click of the button.
<script>
function change() {
document.getElementsByName("savereport")[0].value = 'True';
}
</script>
<form name="test" method="post">
<input type="text" name="savereport" value="False" />
</form>
<div align="center">
<button type="button" value="false" id="check" onclick="change()" >Save</button>
</div>
The code at Question does not set the .value
of <input type="text" name="savereport" value="False" />
element.
You can set the value
of <input type="text" name="savereport" value="false" />
element to "false"
and evaluate boolean false
as a string to check for equality.
<form name="test" method="post">
<input type="text" name="savereport" value="false" />
</form>
<div align="center">
<button type="button" value="false" id="check" onclick="change()">Save</button>
<script>
var input = document.test.savereport;
function change() {
if (input.value === String(false)) {
input.value = true;
// document.test.submit();
} else {
input.value = false;
}
console.log(input.value);
}
</script>
</div>