php - When a specific form option is selected a new input appears, using JavaScript
Get the solution ↓↓↓Solution:
I am not 100% sure if this is the answer that you were seeking but I'll give it a shot.
Instead of programming JavaScript inline you should either use it inside a<script></script>
tag or in a JavaScript file. So what i did was that i set an Event Listener on the select field and what that does is that it listens to when you click, for instance:
var select = document.getElementById('retrieveMarriage');
// Where it says EVENT you can insert any event that you would like
select.addEventListener('EVENT', function() {});
And then you just insert some simple logic into the curly brackets. E.g. naming the hidden divs to something like "box-" followed by one of the numbers that you get fromthis.selectedIndex
.
var select = document.getElementById('retrieveMarriage');
var currentOption = 0;
// Add event listener that listens on when you click "select"
select.addEventListener("click", function() {
// If one of the other options are selected, then hide it and set it to empty
if(currentOption > 0) {
document.getElementById('box-' + currentOption).style.display = 'none';
document.getElementById('box-' + currentOption).firstChild.value = '';
}
// Set current option to be current option
currentOption = this.selectedIndex;
// Set box-N to show
document.getElementById('box-' + currentOption).style.display = 'block';
});
You can check my solution out in this jsfiddle http://jsfiddle.net/6ypytx2p/
Here is a full list of events that you can use. https://developer.mozilla.org/en-US/docs/Web/Events
I hope I was able to answer your question.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.