I have a textbox othervalue
which is enabled/disabled based on the selection of the select
tag.
<select id="capability" name="capability[]" style="background-color: #ffffff;" onchange="selection(this)">
<option value="locMgm">Security</option>
<option value="satelliteComm">Maritime</option>
<option value="othersolution">Other</option>
</select>
Value:
<script type="text/javascript">
$(function() {
$("#capability").change(function() {
if ($(this).val() == "othersolution") {
console.log(true);
$("#othervalue").removeAttr("disabled");
}
else {
console.log(false);
$("#othervalue").attr("disabled", "disabled");
}
});
});
</script>
I am trying to read the value of othervalue
in another JavaScript file:
var othertb = $("#othervalue").val();
and then check whether the textbox
is empty or contains value:
if (othertb == "") {
message = "What solutions are you looking for - no text"
}
else
{
message = "What solutions are you looking for? "+othertb;
}
The problem: othertb
has nothing (empty) and the if else
doesnt work either. How can I read the value and why doesnt the if else
work.
EDIT1: var othertb = $("#othervalue").val();
is unable to read the value of the input field.
All I am trying now is to read the value and show it in an alert(othertb)
to ensure the value is read but it is empty. Can you help read the value?
EDIT2: This is what I am trying on a single file abc.com
:
<td style="background-color: #ffffff;color:#3f3f3f;">
<ol start="4" style="width:auto;" class="container_16" >
<li class="grid_8">What solutions are you looking for?</li></ol>
<select id="capability" name="capability[]" style="background-color: #ffffff;" onchange="selection(this)">
<option value="locMgm">Security</option>
<option value="securityTrc">HSE</option>
<option value="ivms">Transport and Logistics</option>
<option value="satelliteComm">Maritime</option>
<option value="all">Defence</option>
<option value="othersolution">Other</option>
</select>
<label style="font-size:16px" for="value">Value:</label>
<input type="text" id="othervalue" disabled="true"/>
<script type="text/javascript">
var othertb = 'test';
$(function() {
$("#capability").change(function() {
if ($(this).val() == "othersolution") {
console.log(true);
$("#othervalue").removeAttr("disabled");
othertb = $('#othervalue').val();
}
else {
console.log(false);
$("#othervalue").attr("disabled", "disabled");
}
});
});
</script>
</td>
<div id="btn-nxt">
<a href= "javascript:void(0)"
onclick = "alert(othertb)> Submit here »</a>
</div>
When the textbox is empty the page shows alert Test
. When the textbox is not empty the alert
shown has no value and I see am empty alert
pop. Why is the value not being read?