Javascript: Show/Hide rows based on current value of drop-down - javascript

I'm working with Javascript and ASP in this scenario. When this particular page opens, one of my drop-down menus is already populated with a status of "Open" or "Closed" - you can see the value comes from an ID in my recordset.
What I would like to do now is this: If the status on the page when it first loads is "Closed" and the user decides to change it to "Open" they must re-enter a "Reopen Reason" - so, that would display the header and text box below the drop-down....
Here's what I have tried thus far: I have created a showHide() function and placed it inside of the select in the drop-down, but it doesn't do anything, so am now stuck. Any help is appreciated. Thanks!
<select name="cboStatus" id="cboStatus" style="width:200px" onchange="showHide();"> <%
RSStatus.MoveFirst
If Not RSStatus.EOF Then
Do While Not RSStatus.EOF
%><option value='<%= RSStatus("ID")%>'
<%If RSStatus("ID") = RS("prjStatus") Then Response.Write "selected"%>><%= RSStatus("prjStatus")%></option><%
RSStatus.MoveNext
Loop
End If
%>
</select>
The HTML that should be produced from the above JS:
<tr id="lbReopenReason" style="display:none">
<td bordercolor="#f0f0e4" bgcolor="#f0f0e4"><h3>Reopen Reason</h3></td>
</tr>
<tr id="trReopenReason" style="display:none">
<td bordercolor="#FFFFFF" bgcolor="#FFFFFF">
<input name="txtReopenReason" type="text" id="txtReopenReason" value="<%If (RS("reOpenReason")) <> "" Then Response.Write(RS("reOpenReason"))%>" size="100" />
</td>
</tr>
Javascript:
function showHide()
{
var cboStatus = document.getElementById("cboStatus");
var cboStatusValue = cboStatus.options[cboStatus.selectedIndex].text;
var lbReopenReason = document.getElementById("lbReopenReason"); var trReopenReason = document.getElementById("trReopenReason");
//If the status of the project is Closed at the time of page load, and that status changes, then the user must enter a re-open reason.
if ( (status == 3) && (cboStatusvalue == 'Open' )
{
lbReopenReason.style.display = "";
trReopenReason.style.display = "";
}
else
{
lbReopenReason.style.display = "none";
trReopenReason.style.display = "none";
}
}

Looks like there are two problems here:
1) Your function is called "statusShowHide()", not "showHide()", so that could be the reason it's not getting called.
2) Your onchange attribute is missing its closing quotes after the function call, so that could be it as well.
Give those fixes a shot and see if it works now.
EDIT: A few more suggestions:
In your showHide() method you say:
if ( (status == 3) && (cboStatusvalue == 'Open' )
when it should be:
if ( (cboStatus === 3) && (cboStatusValue === 'Open' ) )
Also, instead of the way you're currently getting the value of the element:
var cboStatusValue = cboStatus.options[cboStatus.selectedIndex].text;
Try using:
var cboStatusValue = cboStatus.value;

Have finally resolved this one with the following:
Window onload to populate initial record value:
window.onload = findStatus;
function findStatus()
{
var cboStatus = document.getElementById('cboStatus');
statusShowHide(cboStatus);
}
Followed by this Javascript for changing statuses:
function statusShowHide(obj)
{
var txtStatusFirstLoad = document.getElementById("txtStatusFirstLoad");
var lbReopenReason = document.getElementById("lbReopenReason");
lbReopenReason.style.display = "none";
if (txtStatusFirstLoad.value == 3 && obj.value == 1)
{
lbReopenReason.style.display = "block";
}
}
And added this hidden text box to capture initial status value:
<input name="txtStatusFirstLoad" id = "txtStatusFirstLoad" type="hidden" value="<%=RS("Status")%>" />

Related

When using a form that takes to the main website

I am doing a form window before you get in the main website so I tried to make it so that if you don't fill any of the spaces it will open a window alert asking to fill those spaces. Plus I'd like that you would only get to the main website if you fill all the spaces but yet the button on the form window always takes to the main website without requiring filling of the camps.
On the button I wrote this:
<a href="index1.html">
<input type="button" value="Terminar" onclick = location.href='index1.html' >
</a>
and on the js window I wrote the window alert command to each one of the categories:
if(frm.name.value=="" || frm.name.value==null || frm.name.length < 3) {
alert("Please write your first name ")
frm.name.focus();
return false;
It seems you are trying to validate the an input field based on a few criteria.
Your question is not clear. Is this what you are trying to do?
function validateInput() {
if (frm.value == "" || frm.value == null || frm.value.length < 3) {
alert("Please write your first name ")
frm.focus();
} else
location.href = 'index1.html'
}
<input type="text" id="frm" placeholder="Please write your first name" />
<input type="button" value="Terminar" onClick="validateInput()">
You want something like this. In your code the input is wrapped in an a tag. so it will always trigger the event. Adding a button the trigger the event will help.
button = document.getElementById('enter');
input = document.getElementById('fill');
button.onclick = function() {
if (input.value == null || input.value == "" || input.value == " ") {
alert('Please write your first name');
} else {
window.location.href = 'index1.html';
}
};
<input id="fill">
<button id="enter">Enter</button>

how to disable a check box using java script conditionally

i have 3 check boxes in 3 different pages,i want to check one check box at time means at first all 3 are unchecked and if i checked one check box remaining 2 check boxes should be disable.
each check box value i am storing in 3 different text file using array in the form of 1's and 0's.
now for one page i am reading check box values and based condition trying to disable check box but its not working.
I have Tried this
check box html code:
input type="checkbox" id="cb1" name="check[0]" value="1" />
java script:
<script type="text/javascript">
var cb1 =document.getElementById("cb1");
var cb2 = "<?php echo $cb2_arr[5] ?>" ; //cb2=1 or 0
var cb3 = "<?php echo $cb3_arr[6] ?>"; //cb3=1 or 0
if(cb2 ==1 || cb3 == 1){
cb1.disabled = true;
}else{
cb1.disabled = false;
}
</script>
cb1.disabled = true; for me its not working i kept alert statements above and below it ,only above one is displayed
Please help me how to set disabled property, thanks
try this to disabled and remove disabled,
if ($('#cb3').is(':checked') || $('#cb2').is(':checked')) {
$('#cb1').setAttribute('disabled', true);
}else{
$('#cb1').setAttribute('disabled', false);
}
You need to check input1.value == "", not simply input1 == ""
You also need to fire your method originally, and also run it every time your select lists change value.
Give the function a name
function setCheckState(evt) {
if (input1.value == "" || input2.value == "") {
result.disabled = true;
} else {
result.disabled = false;
}
}
Add event listeners
input1.addEventListener('change', setCheckState);
input2.addEventListener('change', setCheckState);
// Fire the method to get the initial checkbox state set
setCheckState();
Finally, you can reduce your if() statement to a simple assignment...
function setCheckState(evt) {
result.disabled = input1.value == "" || input2.value == "";
}

validating multiple controls in javascript

I am adding multiple controls on an .aspx page from the .vb page based on certain conditions.
My code looks like following:
Dim sb As New StringBuilder
sb.Append("<table border='0'cellpadding='0' cellspacing='0' width='50%' class ='tabledata' id='tblContent'>")
For Each item As myObject In myLst
sb.Append("<tr><td style='width:50%;' valign='top'>")
sb.Append("<textarea id=txt_comments" & i & " name='txt_comments' rows='5' cols='60'></textarea></td>")
sb.Append("<td style='width:15%' valign='top' align='center'><select ID = validate" & i & " name=ValidateValues style ='border:1;width:150px'><option value = ''>Select</option><option value = 'Yes'>Yes</option><option value = 'No'>No</option><br /><br /></td>")
sb.Append("</tr><tr>")
Next
sb.Append("</table>")
myContent.InnerHtml = sb.ToString
So here I am creating <textarea> and <select> dynamically and adding them to my div(myContent)
<div id="structuredContent" runat="server">
</div>
I have a button next where I need to validate for few conditions.
My validation rule is:
User has to select either yes or no from the dropdown(<select>)
If user select 'yes', they have to enter text in
<textarea>(minimum1 character, maximum 1000 characters)
If user select 'No', <textarea> should be disabled.
I am trying to validate like following:
function validateComments() {
var errorcheck = 0;
$("[id^=txt_comments]").each(function () {
var comment = $.trim($(this).val());
$("[id^=validate]").each(function () {
debugger;
var value = $(this).val();
if (comment == 0 && value == "Yes") {
debugger;
errorcheck = 1;
}
});
}); if (errorcheck == 1) {
//show error message
}
else {
ErrorHide();
return true;
}
}
I am able to validate only for one control(which is textarea) from the above code.
The textbox and respective dropdown should be validated along.
How do I add validation for dropdown and can I combine with in the same function.
Any help?
Thanks in advance.
I don't know how do you expect this like if (comment == 0) { to work.
You'll always get a string as a value and checking it with 0 would always return false. Rather you need to check it with "".
And to enable/disable textarea you'll have to attach an event to select tag and do whatever you want to do.
here is an example
$("#d").change(function(){
if($(this).val() === 'n'){
$("#t").prop('disabled', 'disabled')
}else{
$("#t").prop('disabled', false)
}
});
$('body').on('click', '#b', function() {
var text = $.trim($("#t").val());
if(text === "" && !$("#t").prop('disabled')){
alert("yo! not valid")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="d">
<option value="0">Select</option>
<option value="y">Yes</option>
<option value="n">No</option>
</select>
<textarea maxlength="50" id="t"></textarea>\
<button id="b">Validate</button>

How to differentiate a click and button click and non buttonclick using js codecode

I have a webpage where phone number and the dial button appears to be in the same cell of a table . When clicking on the Dial button I should retrieve the phone number in the same cell . This table has dynamically populated and passing this on onclick to javascript is just getting null in js . So I tried the following code instead of the onclick button. The following js code works fine , But it is getting executed clicking anywhere on the cell and not just button . I want to restrict that click to button click . Is there a way to differentiate it ? I cannot use jquery and only want javascript solution since our app is old.
</script>
<script language="javascript">
var tbl = document.getElementById("myTable1");
if (tbl != null) {
for (var i = 0; i < tbl.rows.length; i++) {
tbl.rows[i].cells[0].onclick = function() {
var x = this.cells.item(0).innerHTML;
alert(x);
var res = x.substr(0, 10);
alert(res);
makeCalltoPatient(res);
}
}
}
</script>
<script language="javascript">
html code
<c:forEach items="${contactInfoTabl}" var="element"
varStatus="status">
<tr id="somerow">
<td style="text-align: left" class="ValCntr" id="mytd">
<c:out value="${element.number}" />
<input type="button" class="otherButton" value="Dial" />
</td>
tbl.rows[i].cells[0].onclick = function(e) {
if (e.target.tagName == 'BUTTON') { ...

Toggle visibility of html using javascript checkbox event

I've cobbled together these javascript functions to hide the delivery address fields on my shopping cart address form if goods are going to billing address. The functions toggle visibility of html wrapped by ..
function getItem(id) {
var itm = false;
if(document.getElementById)
itm = document.getElementById(id);
else if(document.all)
itm = document.all[id];
else if(document.layers)
itm = document.layers[id];
return itm;
}
function showHideItem(id) {
itm = getItem(id);
if(!itm)
return false;
if(itm.style.display == 'none')
itm.style.display = '';
else
itm.style.display = 'none';
return false;
}
It works fine if were loading a new address form, the problem I have is if they submit the form with checkbox ticked, and validation fails, the form reloads with the checkbox ticked but unfortunately the fields are visible so now the removing the checkbox hides the fields!!
<tr><td class="white"><strong>Delivery Address</strong></td>
<td>Tick <input Type="checkbox" id="deliver_same" value="yes" onClick="showHideItem('delAddress')" />
If delivery address is billing address</td></tr>
<tbody id="delAddress">
<tr><td>Address line 1</td><td><input class="text" name="saddr1" value="" /></td></tr>
...
<tr><td>Post /Zip Code</td><td><input class="text" name="spostalcode" value="" /></td></tr>
</tbody>
I guess what I need is an onload event which hides the fields if checkbox is ticked when the form loads. Having just written that, I might have a go but not confident. Please, no mention of jquery, its not an option at this point of project.
function checkDeliverSame() {
var deliverSame = getItem('deliver_same');
var deliveryAddress = getItem('delAddress');
if (deliverSame.checked) {
deliveryAddress.style.display = 'none';
} else {
deliveryAddress.style.display = 'block';
}
}
checkDeliverSame(); /* This runs the function on page load */
Put that function, along with your getItem function, right above the </body> tag, and call it in the checkbox input onclick. You will also need to change the id#delAddress element from a tbody to a div so that the getItem function will work on it.
Here's a fiddle: http://jsfiddle.net/JuNhN/1/
I modified Josh's function to make it more generic, prefer document.getElementById() too as it fits in better with itm.style.display. I don't entirely trust checkDeliverSame(); going for a direct call in the html shortly after the closing tag.
function checkHiddenRows(id) {
deliverSame = getItem('deliver_same');
itm = document.getElementById(id);
if (deliverSame.checked == true) {
itm.style.display = 'none';
} // else { alert('Checkbox not checked') } // Verify the checkbox state
}
<script>checkHiddenRows('deliveryAddress');</script>
The form is now working as intended.

Categories