CSS style.display not working - javascript

I have the following JavaScript function to display/hide a table row based on a listbox selection.
function clock_Type(id) {
var clockSource = document.getElementById("clockSource");
var clockType = document.getElementById("clockType");
if(id == "1") {
if(navigator.appName == "Microsoft Internet Explorer") {
clockType.style.display = "inline";
clockSource.style.display = "inline";
} else {
clockType.style.display = "table-row";
clockSource.style.display = "table-row";
}
} else {
clockType.style.display = "none";
clockSource.style.display = "none";
}
}
Following is the listbox.
<select id="clck" name="clock" class="selectStyle" style="width: 155px;" onchange="clock_Type(this.value)">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
This is working fine on the listbox onchange event. i.e. the table row is getting hidden and displayed. But when I try to call the JS function clock_Type() from another function, as shown below, the table row is not getting displayed.
function foo() {
clock_Type(1);
}
When I traced the code the execution reaches the following section for FireFox
clockType.style.display = "table-row";
clockSource.style.display = "table-row";
But the table row is not getting displayed. Any idea what could be the issue here. Thanks in advance!

Set the display to an empty string instead of specifying "table-row" or "inline". This will give the elements a default display, so unless they are specified display:none; in a stylesheet the user agent will negotiate the proper display.
clockType.style.display = "";
clockSource.style.display = "";
This also eliminates the need to execute a browser check which is not good practice.
EDIT:
I misunderstood your question before. However, I tested your code here and it seems to work fine (tested in FF 3.5.3, Chrome 4, IE8).

Does #clockType and #clockSource elements have css class that sets display style? In this case I'd tried to remove it and set display style inline.

Related

Toggle Hiding/Showing an Element

I'm trying to have a couple of buttons to show and hide some pictures, I have gotten it to somewhat work, but when I start the webpage the pictures are already shown, when I try make them invisible at start. I have tried swapping the "block" and "none" sentences in the function, but it just made the button less responsive.
javascript part:
function bassnectar() {
var x = document.getElementById("myDIV3");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
html part:
<button onclick="bassnectar()">Bassnectar</button>
<div id="myDIV3">
<img src="/images/bassnectar.jpg">
</div>
What you’re missing is that you’re assuming that block elements have a default display value of block. Which would be intuitive.
Such is not the case though.
The initial value of display is the browser’s default. If you query the value of the display property on a new HTML element, you’ll get an empty string.
It doesn’t return block until you explicitly set its display to block.
Edit:
It's important to note that setting a value in CSS doesn't change the behavior. If you set a div to be display block in CSS, it will still return an empty string if you query it.
Here's a working example:
var block = document.createElement("div");
var inline = document.createElement("span");
console.log("Initial display values:")
console.log(`block.style.display: ${block.style.display}`);
console.log(`inline.style.display: ${inline.style.display}`);
block.style.display = "block";
inline.style.display = "inline";
console.log("\nAfter setting them explicitly:")
console.log(`block.style.display: ${block.style.display}`);
console.log(`inline.style.display: ${inline.style.display}`);
div {
display: block !important;
}

HTML/Javascript keypad function isn't working

I am making a page for a school project, and I am trying to make a keypad for a passcode system. However, I don't know how to get the keys to work. Looking it up, I have found multiple working keypads, however, I don't know what parts of their code I would have to add to my page to make it work.
The code that follows is currently what I have attached to the onclick part of each of the buttons:
<script>
function kpclick(){
var current = document.getElementById("tbInput").value;
var append = this.innerHTML;
if (current.length < 4) {
if (current=="0") {
document.getElementById("tbInput").value = append;
} else {
document.getElementById("tbInput").value += append;
}
}
}
</script>
The "tbInput" mentioned is a textbox underneath the keypad in the same div that later will be hidden and (hopefully) disabled. If there is no solution that allows the keypad to be used while the textbox is disabled, I will likely move it outside of the div the keypad is in.
When I press any of the buttons currently, nothing happens. What's happening here?
I'm assuming this is meant to be the button context. You can pass it with onclick="kpclick(this)".
function kpclick(target) {
var current = document.getElementById("tbInput").value;
var append = target.innerHTML;
if (current.length < 4) {
if (current == "0") {
document.getElementById("tbInput").value = append;
} else {
document.getElementById("tbInput").value += append;
}
}
}
<button onclick="kpclick(this)">0</button>
<button onclick="kpclick(this)">1</button>
<input id="tbInput">

How to auto-focus after changing display from none to block?

I have a list of inputs under a list of divs respectively. I have a button that when clicked it will switch one input from from not display to display. This is something like:
if (switched) {
document.getElementById("div-xxx").style.display = "block";
}
However, is there a way I could make the input inside the displayed div being auto focused after this switch? I tried something like
document.getElementById('input-xxx').autofocus = true;
after the display code, but there is no autofocus at all.
document.getElementById('input-xxx').focus() will change the focus to the selected element.
document.getElementById('input-xxx').setAttribute('autofocus', true) will assign the autofocus attribute to the html element
object.focus(); will help
if (switched) {
document.getElementById('div-xxx').style.display = "block";
document.getElementById('input-xxx').focus();
}
The only thing that worked for me was
if (switched) {
document.getElementById('div-xxx').style.display = "block";
document.getElementById('old-input').setAttribute('autofocus', false);
document.getElementById('input-xxx').setAttribute('autofocus', true);
document.getElementById('input-xxx').focus();
}

HTML/Jscript/Contact Form 7: Easiest way to hide several labels at once

I have the following piece of code i'm working on, I want to find an easy way to hide all the labels after (and including) Text2 whenever Option1 or Option2 are selected on the drop menu. I tried working around with class elements but they seem to break my script, right now I am only able to hide one label only (text5). Would you mind helping me?
<p><label>Text1</label> [select* id:mobileProvider "Option1" "Option2"</p>
<p><label>Text2</label></p>
<p><label>Text3</label></p>
<p><label>Text4</label></p>
<p><label id="sub">Text5</label></p>
<script language="javascript" type="text/javascript">
document.getElementById("mobileProvider").addEventListener("change", displayTextField);
function displayTextField() {
var dropDownText = document.getElementById("mobileProvider").value;
if (dropDownText == "Option1") {
document.getElementById("sub").style.display = 'none';
} else if (dropDownText == "Option2") {
document.getElementById("sub").style.display = 'none';
} else {
document.getElementById("sub").style.display = 'inline';
}
}
</script>
I changed the 'class' tag in every paragraph to 'hid' and added the next row in my script, and it did the trick.
var hide = document.getElementsByClassName('hid'),i = hide.length;
while(i--) {
hide[i].style.display= 'none'; }

Calling a function when checking a checkbox, onclick event doesn't fire when unchecking

I should probably start by mentioning that I am using Internet Explorer 6. I am calling a JavaScript function (tabModifiedHighlight) from an onChange event. The function works perfectly other places however, I have a couple of places on the page where it works when I check the checkbox, but the event doesn't even seem to fire when I uncheck it.
Here is the JavaScript function:
function tabModifiedHighlight(){
alert("alert");
var div, i, input, inputIndex, selects, selectIndex, selectedTab, highlighted;
var tabs = new Array("admissioninformation","diet","vitalsigns","activities","nursing","ivfluids","medications1","medications2","labs","respiratory","diagnostic","consultations");
for(i=0; i<(tabs.length); i++){
selectedTab = tabs[i]+'tab';
if (document.getElementById(selectedTab).className == "selectedtab"){
div = document.getElementById(tabs[i]),
input = div.getElementsByTagName('input'),
selects = div.getElementsByTagName('select');
break;
}
}
highlighted = false;
for (inputIndex = 0; inputIndex < input.length; inputIndex++){
if (input[inputIndex].checked == true){
highlighted = true;
}
}
for (inputIndex = 0; inputIndex < input.length; inputIndex++){
if (input[inputIndex].type == 'text' && input[inputIndex].value != ""){
highlighted = true;
}
}
for (selectIndex = 0; selectIndex < selects.length; selectIndex++){
if (selects[selectIndex].value != ""){
highlighted = true;
}
}
if (highlighted == true){
document.getElementById(selectedTab).style.backgroundColor = "#FF0";
}
else {
document.getElementById(selectedTab).style.backgroundColor = "#F0F0F0";
}
}
And here is the input that is calling it:
<input name="cbMedTylenolPO" id="cbMedTylenolPO" type="checkbox" value="PO" onClick="tylenolPoShowHide(); checkBoxHighlight(this, 'MedicationsRow2'); tabModifiedHighlight();" />
This page has multiple "tabs" which are just divs that are set to visible or hidden based on which one is selected. It seems consistent in that it works everywhere except for 2 of the tabs, and nowhere on those tabs. The only other difference I can see is that the ones that are not working are also showing or hiding divs within the tab, based on whether the checkbox is checked or not. I have added the alert at the very beginning of the function to see if it is firing or not, and it does when checking the checkbox, but not when unchecking.
I hope I made this clear, and any thoughts are appreciated!
As your code is not working only for two tabs, and working for all others its not an browser compatibility issue.
onClick if checkbox you are calling these 3 methods
tylenolPoShowHide(); checkBoxHighlight(this, 'MedicationsRow2');tabModifiedHighlight()
Note tabModifiedHighlight is last one..
if any of first two methods tylenolPoShowHide or checkBoxHighlight fails... then tabModifiedHighlight will not be called.
I will suggest to add alert as first and last line in both tylenolPoShowHide and checkBoxHighlight ...
It will help you find which one is actually failing then you can add that code here and we will be able to help you further

Categories

Resources