show different form with radio buttons - javascript

In my HTML I have 4 fieldsets.
In the second fieldset there are 2 radio buttons. The first one needs to show the thirth fieldset and the second one needs to show the 4th fieldset.
Now the problem is that the second radio button does it's job. It shows the right fieldset and it hides when the first radio button is selected. But the first radio button does nothing anymore.
It won't show the thirth fieldset. This is since I added the classList.remove for the fieldset2 var.
I don't know why this line of code permanently hides the thirth fieldset.
I just found out that the first var gets declined completely. Only the last var will be executed.
Even if I put them in different files or different functions and even if I make different classes for them in css the var will overwrite the first one...
function form
<form>
<fieldset>
<legend>Contactgegevens</legend>
<label for="naam">Naam</label>
<input type="text" id="naam" required/>
<label for="bedrijf">Naam bedrijf</label>
<input type="email" id="bedrijf" required/>
<label for="adres">Adres</label>
<input type="text" id="adres" required/>
<label for="postcode">Postcode</label>
<input type="text" pattern="[1-9][0-9]{3}\s?[a-zA-Z]{2}" id="postcode" placeholder="1234AB" required/>
<label for="plaats">Plaatsnaam</label>
<input type="number" id="plaats" required/>
<label for="telefoon">Telefoon</label>
<input type="tel" id="telefoon" />
<label for="land">E-mail</label>
<input type="text" id="land" placeholder="voorbeeld#email.com" required/>
</fieldset>
<fieldset>
<legend>Ik wil mij aanmelden voor:</legend>
<label for="project">Een project</label>
<input type="radio" name="aanmelden" id="project"/>
<label for="stage">Een stage</label>
<input type="radio" name="aanmelden" id="stage"/>
</fieldset>
<fieldset>
<legend>Project</legend>
<label for="titel">Titel project</label>
<input type="text" id="titel" />
<label for="omschrijving">Opdrachtomschrijving</label>
<textarea id="omschrijving" rows="6" cols="25"></textarea>
<label for="doelgroep">Doelgroepomschrijving</label>
<textarea id="doelgroep" rows="6" cols="25"></textarea>
<label for="intentie">Intentie van het project</label>
<textarea id="intentie" rows="6" cols="25"></textarea>
<label for="looptijd">Looptijd</label>
<input type="text" id="looptijd" placeholder="Bijv: 10 weken"/>
<label for="deadline">Deadline</label>
<input type="text" id="deadline" placeholder="dd-mm-jjjj"/>
<label for="geschikt">Geschikt voor</label>
<select id="geschikt">
<option>Eerstejaar studenten</option>
<option>Tweedejaars studenten</option>
<option>Derdejaars studenten</option>
<option>Afstudeer studenten</option>
<option>Onbekend</option>
</select>
</fieldset>
<fieldset>
<legend>Stage</legend>
<label for="geschikt2">Geschikt voor</label>
<select id="geschikt2">
<option>Tweedejaars studenten</option>
<option>Afstudeer studenten</option>
<option>Onbekend</option>
</select>
<label for="hoelang">Hoelang is de stage?</label>
<select id="hoelang">
<option>10 weken</option>
<option>20 weken</option>
</select>
<label for="begindatum">Begindatum</label>
<input type="text" id="begindatum" placeholder="dd-mm-jjjj"/>
<label for="werkzaamheden">Omschrijving stagewerkzaamheden</label>
<textarea id="werkzaamheden" rows="6" cols="25"></textarea>
</fieldset>
</form>
Keuze(){
console.log("hoi")
var fieldset = document.querySelector('fieldset:nth-of-type(3)');
fieldset.classList.add('hidefield');
document.querySelector('input[type="radio"]:first-of-type').onclick = function() {
fieldset.classList.add('showfield');
}
document.querySelector('input[type="radio"]:last-of-type').onclick = function() {
fieldset.classList.remove('showfield');
}
var fieldset2 = document.querySelector('fieldset:nth-of-type(4)');
fieldset2.classList.add('hidefield');
document.querySelector('input[type="radio"]:last-of-type').onclick = function() {
fieldset2.classList.add('showfield');
}
document.querySelector('input[type="radio"]:first-of-type').onclick = function() {
fieldset2.classList.remove('showfield');
}
}
window.onload=formKeuze;

Related

Change a CSS display style in Javascript

I have a problem with a bit of my javascript code.
I am trying to get my #showAddress to display: block when the deliverservice radio button is clicked/checked.
I've tried looking at some Stack Overflow posts but a lot of them are too advanced for my current level (I just started JavaScript).
<label>Delivery Type</label>
<input type="radio" name="delivery" value="Pickup" id="pickupService">Pickup
<input type="radio" name="delivery" value="Deliver" id="deliverService">Deliver
</p>
<div id="showAddress">
<p>
<label for="deliveryAddress">Delivery Address</label><br>
<input type="text" name="deliveryAddress" id="deliveryAddress" placeholder="Enter street number + name"><br>
<input type="text" name="deliverySuburb" id="deliverySuburb" placeholder="Enter your [suburb], [state]"><br>
<input type="text" name="deliveryAddress" id="deliveryPostcode" placeholder="Enter your postcode"><br>
Billing address same as Above
</div>
<p>
<label for="billingAddress">Billing Address</label><br>
<input type="text" name="deliveryAddress" id="billingAddress" placeholder="Enter street number + name"><br>
<input type="text" name="deliverySuburb" id="billingSuburb" placeholder="Enter your [suburb], [state]"><br>
<input type="text" name="deliveryAddress" id="billingPostcode" placeholder="Enter your postcode"><br>
</p>
<p>
I believe the error is happening in my JavaScript, however it's only two functions. The first two lines are my initialise function, which is calling my second function down below.
var showDelivery = document.getElementById("deliverService");
showDelivery.onclick = showAddress;
function showAddress() {
document.getElementById("showAddress").style.display= 'block';
}
#additionalInformation, #showAddress {
display: none;
}
You should change this line
showDelivery.onclick = showAddress;
to something like this:
showDelivery.onclick = function(){showAddress();};
Alternatively, you can set the onclick listener on the input element itself (in the html), to directly call the javascript function from the HTML element, rather than cluttering your JavaScript
<input type="radio" name="delivery" value="Deliver" id="deliverService" onclick="showAddress()">Deliver
function showAddress() {
document.getElementById("showAddress").style.display= 'block';
}
#additionalInformation, #showAddress {
display: none;
}
<label>Delivery Type</label>
<input type="radio" name="delivery" value="Pickup" id="pickupService">Pickup
<input type="radio" name="delivery" value="Deliver" id="deliverService" onclick="showAddress()">Deliver
<div id="showAddress">
<p>
<label for="deliveryAddress">Delivery Address</label><br>
<input type="text" name="deliveryAddress" id="deliveryAddress" placeholder="Enter street number + name"><br>
<input type="text" name="deliverySuburb" id="deliverySuburb" placeholder="Enter your [suburb], [state]"><br>
<input type="text" name="deliveryAddress" id="deliveryPostcode" placeholder="Enter your postcode"><br>
Billing address same as Above
</p>
</div>
<p>
<label for="billingAddress">Billing Address</label><br>
<input type="text" name="deliveryAddress" id="billingAddress" placeholder="Enter street number + name"><br>
<input type="text" name="deliverySuburb" id="billingSuburb" placeholder="Enter your [suburb], [state]"><br>
<input type="text" name="deliveryAddress" id="billingPostcode" placeholder="Enter your postcode"><br>
</p>
EDIT: An example of using addEventListener
document.getElementById("deliverService").addEventListener("click", function() {
document.getElementById("showAddress").style.display= 'block';
});
document.getElementById("deliverService").addEventListener("click", function() {
document.getElementById("showAddress").style.display= 'block';
});
#additionalInformation, #showAddress {
display: none;
}
<label>Delivery Type</label>
<input type="radio" name="delivery" value="Pickup" id="pickupService">Pickup
<input type="radio" name="delivery" value="Deliver" id="deliverService">Deliver
<div id="showAddress">
<p>
<label for="deliveryAddress">Delivery Address</label><br>
<input type="text" name="deliveryAddress" id="deliveryAddress" placeholder="Enter street number + name"><br>
<input type="text" name="deliverySuburb" id="deliverySuburb" placeholder="Enter your [suburb], [state]"><br>
<input type="text" name="deliveryAddress" id="deliveryPostcode" placeholder="Enter your postcode"><br>
Billing address same as Above
</p>
</div>
<p>
<label for="billingAddress">Billing Address</label><br>
<input type="text" name="deliveryAddress" id="billingAddress" placeholder="Enter street number + name"><br>
<input type="text" name="deliverySuburb" id="billingSuburb" placeholder="Enter your [suburb], [state]"><br>
<input type="text" name="deliveryAddress" id="billingPostcode" placeholder="Enter your postcode"><br>
</p>

Angularjs enable and disable all textboxes when i empty one

I am beginner in web development and I want to learn more about how I can do controls on my inputs and select.
I have one select and four input.
I want to disable all other input and the select if I select an option or I enter a text in my input
Sorry for my English, can you help me with the AngularJS part, it's important I want to do it with AngularJS.
When the other inputs of selects are disabled I need to display a message for explain why it's impossible to use other input or select
I don't know how I can do, but I think I need to use ng-model , ng-disabled, ng-change, maybe ng-blur and ng-focus
<form>
<div class="form-group">
<label for="Select1">Choix</label>
<select class="form-control" id="exampleFormControlSelect1">
<option>choix 1</option>
<option>choix 2</option>
<option>choix 3</option>
<option>choix 4</option>
<option>choix 5</option>
</select>
</div>
<div class="form-group">
<label for="Input1">Choix A</label>
<input type="text" class="form-control" id="Input1" placeholder="Choix A">
</div>
<div class="form-group">
<label for="Input2">Choix B</label>
<input type="text" class="form-control" id="Input2" placeholder="Choix B">
</div>
<div class="form-group">
<label for="Input3">Choix C</label>
<input type="text" class="form-control" id="Input3" placeholder="Choix C">
</div>
<div class="form-group">
<label for="Input4">Choix D</label>
<input type="text" class="form-control" id="Input4" placeholder="Choix D">
</div>
</form>
Thanks for your future help .
You can use ng-model in selectedChoix to get the value of selected option and ng-disabled if there have no selectedChoix.
Choix
choix 1
choix 2
choix 3
choix 4
choix 5
<div class="form-group">
<label for="Input1">Choix A</label>
<input type="text" class="form-control" id="Input1" placeholder="Choix A" ng-disabled="!selectedChoix">
</div>
<div class="form-group">
<label for="Input2">Choix B</label>
<input type="text" class="form-control" id="Input2" placeholder="Choix B" ng-disabled="!selectedChoix">
</div>
<div class="form-group">
<label for="Input3">Choix C</label>
<input type="text" class="form-control" id="Input3" placeholder="Choix C" ng-disabled="!selectedChoix">
</div>
<div class="form-group">
<label for="Choix D">Email address</label>
<input type="text" class="form-control" id="Input4" placeholder="Choix D" ng-disabled="!selectedChoix">
</div>
This will automatically disabled other input fields until select any option.When select a option then other input fields will enable to write.
you can write your message anywhere until select any option or can use tooltip.

Radio button selection for paypal form issue

need your help
So i have a paypal form which also has two radio buttons. One with a shipping value, and one without. Here is the link to the actual form
http://www.topchoicedata.com/test.php
What i want to happen is that if a person selected the first radio box("$19.99 Shipping – DATABASE SENT ON CD ROM"), then 19.99 would automatically be added to the "hdwppamount" amount which is set at $175, so when the person press's "pay now", it will make a grand total of $194.99 on the paypal page.
And if the person selected the second option instead("FREE SHIPPING – DATABASE SENT VIA EMAIL") instead, then of course no value is added to the "hdwppamount" amount and $175 would be the total for the paypal page.
Now the code does work if the person chooses on or the other options, and then does not alternate between the radio buttons deciding which one they choose. So its kind of buggy because if i choose the first option, then decided the second option, and so on and so forth alternating, i either get an error page or if i did choose the free option, the 19.99 still gets added.
I need so that if the first option is chosen, then the 19.99 gets added, and if the second option chosen, then nothing is added.
Any help would be appreciated.
<!--------------------HTML Form---------------------_
<form action="PPPaymentForm/PPPaymentForm.php" method="post" name="topchoiceform" id="topchoiceform">
<input placeholder="Company Name" type="text" name="companyname" required>
<input placeholder="First Name" type="text" name="first_name" required>
<input placeholder="Last Name" type="text" name="last_name" required>
<input placeholder="Email" type="email" name="email" id="email" required>
<input placeholder="Address" type="text" name="address1" required>
<input name="shipping" class="shipping" type="radio" id="shipping" checked/>
<label class="shippingcheck">$19.99 Shipping – DATABASE SENT ON CD ROM </label>
<br>
<input name="shipping" class="shipping" type="radio" id="noshipping"/>
<label class="shippingcheck">FREE SHIPPING – DATABASE SENT VIA EMAIL</label>
<br>
<button name="submit" type="submit" id="submit">Pay Now</button>
<!-------------Paypal Part------------->
<input type="hidden" name="hdwtablename" id="hdwtablename" value="1">
<input type="hidden" name="hdwppproductname" id="hdwppproductname" value="Basic Plan 175">
<input type="hidden" name="hdwppamount" id="hdwppamount" value="175">
<input type="hidden" name="hdwppcurrency" id="hdwppcurrency" value="USD">
<input type="hidden" name="hdwpplanguage" id="hdwpplanguage" value="en_US">
<input type="hidden" name="hdwok" id="hdwok" value="http://www.topchoicedata.com/paymentmadethanks.php">
<input type="hidden" name="hdwemail" id="hdwemail" value="mauriceflopez+gmail.com">
<input type="hidden" name="hdwnook" id="hdwnook" value="http://">
<input type="hidden" name="hdwactivation_email" id="hdwactivation_email" value="email">
<input type="hidden" name="plan" id="plan" value="$175">
<input type="hidden" name="shippingchoosen" id="shippingchoosen" value="">
<input type="hidden" name="shippingnumber" id="shippingnumber" value="">
</form>
PHP
<script>
$('#shipping').change(function(){
var hdwppamount = Number($("#hdwppamount").val())
var shippingcost = 19.99;
if (this.checked) {
$("#hdwppamount").val(hdwppamount+shippingcost)
} else {
$("#hdwppamount").val(hdwppamount-shippingcost)
}
})
</script>
With radio you may have only one of them checked at a time. This means you have to test which one is checked in a specified moment (i.e.: change event).
I changed the input field hdwppamount from hidden to text in the snippet to make clear the behaviour.
$(function () {
$('#shipping, #noshipping').on('change', function(){
var hdwppamount = Number($("#hdwppamount").val());
var shippingcost = 19.99;
if (this.id == 'noshipping') { // shipping unchecked
$("#hdwppamount").val(hdwppamount-shippingcost);
} else { // shipping checked
$("#hdwppamount").val(hdwppamount+shippingcost);
}
});
// initialize the field with the correct value
$("#hdwppamount").val('194.99');
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<form action="PPPaymentForm/PPPaymentForm.php" method="post" name="topchoiceform" id="topchoiceform">
<input placeholder="Company Name" type="text" name="companyname" required>
<input placeholder="First Name" type="text" name="first_name" required>
<input placeholder="Last Name" type="text" name="last_name" required>
<input placeholder="Email" type="email" name="email" id="email" required>
<input placeholder="Address" type="text" name="address1" required>
<input name="shipping" class="shipping" type="radio" id="shipping" checked/>
<label class="shippingcheck">$19.99 Shipping – DATABASE SENT ON CD ROM </label>
<br>
<input name="shipping" class="shipping" type="radio" id="noshipping" />
<label class="shippingcheck">FREE SHIPPING – DATABASE SENT VIA EMAIL</label>
<br>
<button name="submit" type="submit" id="submit">Pay Now</button>
<!-------------Paypal Part------------->
<input type="hidden" name="hdwtablename" id="hdwtablename" value="1">
<input type="hidden" name="hdwppproductname" id="hdwppproductname" value="Basic Plan 175">
<input type="text" name="hdwppamount" id="hdwppamount" value="175">
<input type="hidden" name="hdwppcurrency" id="hdwppcurrency" value="USD">
<input type="hidden" name="hdwpplanguage" id="hdwpplanguage" value="en_US">
<input type="hidden" name="hdwok" id="hdwok" value="http://www.topchoicedata.com/paymentmadethanks.php">
<input type="hidden" name="hdwemail" id="hdwemail" value="mauriceflopez+gmail.com">
<input type="hidden" name="hdwnook" id="hdwnook" value="http://">
<input type="hidden" name="hdwactivation_email" id="hdwactivation_email" value="email">
<input type="hidden" name="plan" id="plan" value="$175">
<input type="hidden" name="shippingchoosen" id="shippingchoosen" value="">
<input type="hidden" name="shippingnumber" id="shippingnumber" value="">
</form>

AngulaJS input fields not loose focus when clicked

I am trying to create a simple angularjs form where i want to have nested object as ng-model
$scope.project = {
name:"Some Name",
location:{line1:"" , line2:"", city:"", zipcode:""}
}
http://plnkr.co/edit/RfN7qZBX3HlOtGhFOdFX?p=preview
now the problem is when i click on line2 , city,state etc focus goes back to line1
tried changing HTML and several other stuff but don't know what to do..
Tried removing bootstrap as well.
The issue is that you are misusing the <label> tag. Instead of this:
<label class="form-group">
Client Location
<div class="controls">
<input type="text" data-ng-model="project.location.line1" class="form-control" placeholder="Line 1">
<input type="text" data-ng-model="project.location.line2" class="form-control" placeholder="Line 2">
<input type="text" data-ng-model="project.location.city" class="form-control" placeholder="City">
<input type="text" data-ng-model="project.location.state" class="form-control" placeholder="State">
<input type="text" data-ng-model="project.location.zip" class="form-control" placeholder="Zip Code">
<input type="text" data-ng-model="project.location.country" class="form-control" placeholder="Country">
</div>
</label>
Try this:
<div class="form-group">
<label>Client Location</label>
<div class="controls">
<input type="text" data-ng-model="project.location.line1" class="form-control" placeholder="Line 1">
<input type="text" data-ng-model="project.location.line2" class="form-control" placeholder="Line 2">
<input type="text" data-ng-model="project.location.city" class="form-control" placeholder="City">
<input type="text" data-ng-model="project.location.state" class="form-control" placeholder="State">
<input type="text" data-ng-model="project.location.zip" class="form-control" placeholder="Zip Code">
<input type="text" data-ng-model="project.location.country" class="form-control" placeholder="Country">
</div>
</div>
The first label should be changed as well. Instead of this:
<label class="form-group">Name
<div class="controls">
<input type="text" data-ng-model="project.name" class="form-control" placeholder="Name">
</div>
</label>
Try this:
<div class="form-group">
<label>Name</label>
<div class="controls">
<input type="text" data-ng-model="project.name" class="form-control" placeholder="Name">
</div>
</div>
Or this:
<div class="form-group">
<label>
Name
<input type="text" data-ng-model="project.name" class="form-control" placeholder="Name">
</label>
</div>

How to I only show the divs if that div has an input that isn't blank?

I have a bunch of divs that are all hidden like this
<div class="elements">
<input value="something" type="text" value="" name="name">
<input value="" type="text" name="phone">
</div>
<div class="elements">
<input type="text" value="" name="name" class="contact_name">
<input value="something" type="text" name="phone">
</div>
<div class="elements">
<input type="text" value="" name="name" class="contact_name">
<input value="" type="text" name="phone">
</div>
<div class="elements">
<input value="something_again" type="text" value="" name="name">
<input value="" type="text" name="phone">
</div>
CSS
.elements{display:none;}
I want to show only the element divs that have an input of that is not an empty string....so in the above example i would show the first, second and fourth divs because at least one input has a value...
$('.elements').find('input').each(function)...
that is what i have so far but not sure how to search if there is at least one input that isn't blank
Try this
$('.elements input:text[value!=""]').parents(".elements").show();
$('.elements').each(function() {
var count = $(this).find('input[value!=""]').length;
count > 0 ? $(this).show() : $(this).hide();
});

Categories

Resources