i would like to click in plus ,then show the next field in every step but my code does not work in third step .is there any method to do these steps for unlimited time?tnx
<html>
<head>
<script>
function showfield(){
document.getElementById("hiddenfield").style.display="block";
}
</script>
</head>
<body>
<div class="form-group" >
<div class="rightbox"> <label for='phone'>Phone1</label></div>
<input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div>
</div>
<div class="form-group" style="display:none;" id="hiddenfield">
<div class="rightbox"><label for='phone'>Phone2</label></div>
<input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div></div>
</div>
<div class="form-group" style="display:none;" id="hiddenfield">
<div class="rightbox"><label for='phone'>Phone3</label></div>
<input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div></div>
</div>
</body>
</html>
You can use unique id for each hidden field, then send current id to your function as parameter.
function showfield(id) {
document.getElementById(id).style.display="block";
}
Another option is jquery:
$('.plus').on('click', function() {
$(this).parent().next().show();
});
Check out jsfiddle.
As others noted, ID of an element must be unique, you can use class to group similar elements.
What you need to do is to show the first hidden field group,
function showfield() {
var el = document.querySelector(".hiddenfield");
if (el) {
el.classList.remove('hiddenfield')
}
}
.hiddenfield {
display: none;
}
<div class="form-group">
<div class="rightbox">
<label for='phone1'>Phone1</label>
</div>
<input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div>
</div>
<div class="form-group hiddenfield">
<div class="rightbox">
<label for='phone2'>Phone2</label>
</div>
<input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div>
</div>
<div class="form-group hiddenfield">
<div class="rightbox">
<label for='phone3'>Phone3</label>
</div>
<input type="tel" value="" />
<div class="plus" onClick="showfield()">+</div>
</div>
Supported in IE10+
Since you've tagged jquery, you could do away with all those (duplicate ids) and showField() and within doc load add this:
$('.plus').on('click', function() {
$(this).parent().next().show();
});
[Edit: Oh, here's the fiddle]
However, I'm not sure what you want to happen when the third + is clicked as there are no more divs to show.
Change id="hiddenfield" to class="hiddenfield" in all divs and change showfield as below :-
function showfield() {
document.getElementsByClassName("hiddenfield").style.display="block";
}
Note:- Supported Browsers IE9+, Chrome 4+, FF3+.
http://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp
Related
I have a button click function like this :
$("#submitButton").click(function (e) {
e.preventDefault();
console.log("let's show my div");
$('#mydiv').show();
//and then doing a lot of front end operations and some ajax calls
})
When I click the submit button, I get the console.log message immediately. But .show() method works like 7-8 seconds after that. Can you tell me how I can make .show() work immediately? Thanks.
EDIT :
My HTML code looks like this :
<div class="main">
<form id="myform" enctype="multipart/form-data" class="contact-forms">
<div class="first-line">
<div class="span3 main-row">
<div class="input">
<input type="text" id="id" name="id" placeholder="insert your ID" maxlength="7" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" />
</div>
</div>
</div>
<div class="first-line">
<div class="span8 main-row">
<div class="input">
<input type="text" id="name" name="name" placeholder="Your name" />
</div>
</div>
</div>
<div id="mydiv" style="display:none">
<label>
Processing, please wait.
</label>
</div>
</form>
</div>
This is example of showing the div , you should hide your div with display none not hidden class , if you use hidden class just remove class to show your div
function ShowDiv(){
$("#mydiv").show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mydiv" style='display:none;'>Hello</div>
<button onclick="ShowDiv()"> ShowMe</button>
On click of checkbox I want to hide one last input field. I have tried prev and closest but it is not working. There are few other set of same kind of input fields so I just want to target on last input field. So on click of this checkbox I want to hide last input field job_to_date[]. Below is my code.
jQuery('input[name^="currently_work_here"]').change(function() {
if (jQuery(this).is(":checked")) {
jQuery(this).prev('.job_to_date').remove();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-6">
<input type="text" name="job_from_date[]" value="" placeholder="From" class="job_from_date">
</div>
<div class="col-sm-6">
<input type="text" name="job_to_date[]" value="" placeholder="To" class="job_to_date">
</div>
<div class="col-sm-12">
<input type="checkbox" name="currently_work_here[]" class="resume_builder_input input_checkbox"> I currently work here
</div>
Based on your revised HTML you need to use .parent().prev().find('.job_to_date') to locate the element you want to remove:
jQuery('input[name^="currently_work_here"]').change(function() {
if(jQuery(this).is(":checked")) {
jQuery(this).parent().prev().find('.job_to_date').remove();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-sm-6">
<input type="text" name="job_from_date[]" value="" placeholder="From" class="job_from_date">
</div>
<div class="col-sm-6">
<input type="text" name="job_to_date[]" value="" placeholder="To" class="job_to_date">
</div>
<div class="col-sm-12">
<input type="checkbox" name="currently_work_here[]" class="resume_builder_input input_checkbox"> I currently work here
</div>
.prev() alone won't work based on your structure. You need to go up a level via .parent(), then to the previous div with .prev(), finally finding the one to remove with .find('.job_to_date')
Ok, I've been having a really weird problem using a checkbox which collapses a hidden div using bootstrap.
if I have data-toggle="collapse" in the checkbox input attribute section, the Div Collapses but requires that every single one of the inputs inside it be filled out.
If data-toggle="collapse" is not there, the hidden div doesn't collapse, and if the checkbox is checked it requires the inputs to be entered and if it's left unchecked I can submit the form without the inputs being entered. (desired action, but the div doesn't hide or show when the checkbox is checked)
How do I hide/show the div when the checkbox is unchecked/checked AND only require the inputs if the box is checked?
I'm using this as the HTML:
<div class="col-md-1">
<input type="checkbox" onclick="ChangeShip()" href="#moreabout" data-toggle="collapse" aria-expanded="false" aria-controls="moreabout" class="form-control" id="chShipAdd" name="chShipAdd" value="no">
</div>
<label for="chShipAdd" class="col-md-3 control-label">Shipping Information?</label>
<div id="shipadddiv" style="visibility: hidden;">
<div class="collapse" id="moreabout" >
<div class="form-group">
<div class="col-md-12">
<br>
<input id="sStreet" name="sStreet" type="text" placeholder="Street Name (required)" class="form-control shipClass" required>
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<input id="sCity" name="sCity" type="text" placeholder="City (required)" required class="form-control shipClass">
</div>
<div class="col-md-4">
<input id="sState" name="sState" type="text" placeholder="State (required)" required class="form-control shipClass">
</div>
<div class="hidden-lg hidden-md"> </div>
<div class="col-md-4">
<input id="sZipcode" name="sZipcode" type="text" placeholder="Zip (required)" required class="form-control shipClass">
</div>
</div>
</div>
</div>
and the javascript:
function ChangeShip() {
if (!(document.getElementById('chShipAdd').checked)) {
document.getElementById('shipadddiv').style.visibility="hidden";
$(".shipClass").prop("disabled",true);
}
else {
document.getElementById('shipadddiv').style.visibility="visible";
$(".shipClass").prop("disabled",false);
}
}
Any solution that WORKS will be acceptable. I've bashed my brain all day trying to do this simple action. I've tried .prop .attribute .setAttribute .removeAttribute, and much much more.
Any Advice?
You can use jquery to solve this quickly. You can wrap your inputs for change ship and give it and id. And let jquery do the rest.
var form = $('#myForm'),
checkbox = $('#changeShip'),
chShipBlock = $('#changeShipInputs');
chShipBlock.hide();
checkbox.on('click', function() {
if($(this).is(':checked')) {
chShipBlock.show();
chShipBlock.find('input').attr('required', true);
} else {
chShipBlock.hide();
chShipBlock.find('input').attr('required', false);
}
});
See this jsfiddle for your problem. This should help you.
your click event will toggle the display and the disabled, but when the form is loaded you will have hidden content that is not disabled.
simply call the function on document.ready
function ChangeShip() {
var show = $('#chShipAdd').prop('checked');
$('#shipadddiv').toggle(show);
$("#shipadddiv .shipClass").prop("disabled", !show);
}
$(ChangeShip); // call on document.ready
or simply add the disabled attribute to those elements so that the initial form state is valid
If the [required] attribute is still triggered on a [disabled] element you could juggle the attribute value
function ChangeShip() {
var show = $('#chShipAdd').prop('checked');
$('#shipadddiv').toggle(show);
$("#shipadddiv .shipClass").each(function(){
if (!('_required' in this))
this._required = this.required;
this.disabled = !show;
this.required = (show) ? this._required : false;
});
}
Try to do this :
HTML :
<div class="col-md-1">
<input type="checkbox" onclick="ChangeShip()" href="#moreabout" data-toggle="collapse" aria-expanded="false" aria-controls="moreabout" class="form-control" id="chShipAdd" name="chShipAdd" value="no">
</div>
<label for="chShipAdd" class="col-md-3 control-label">Shipping Information?</label>
<div id="shipadddiv" style="visibility: hidden;">
<div class="collapse" id="moreabout" >
<div class="form-group">
<div class="col-md-12">
<br>
<input id="sStreet" name="sStreet" type="text" placeholder="Street Name (required)" class="form-control shipClass" required>
</div>
</div>
<div class="form-group">
<div class="col-md-4">
<input id="sCity" name="sCity" type="text" placeholder="City (required)" required class="form-control shipClass">
</div>
<div class="col-md-4">
<input id="sState" name="sState" type="text" placeholder="State (required)" required class="form-control shipClass">
</div>
<div class="hidden-lg hidden-md"> </div>
<div class="col-md-4">
<input id="sZipcode" name="sZipcode" type="text" placeholder="Zip (required)" required class="form-control shipClass">
</div>
</div>
</div>
</div>
JQUERY :
$('#chShipAdd').change(function() {
if ($('#chShipAdd').prop('checked')) {
$('#shipadddiv').show();
} else {
$('#shipadddiv').hide();
}
});
How can I only affect the element that when I focus on the input type the error message below it should be gone, problem is when I focus on the input type the error message hides all. Hope you can help me. Thanks in advance
This is my HTML structure
<div class="form-group">
<input class="form-control" type="text" />
<div class="form-message">Please all fields </div>
</div>
<div class="form-group">
<input class="form-control" type="text" />
<div class="form-message">Please all fields </div>
</div>
<div class="form-group">
<input class="form-control" type="text" />
<div class="form-message">Please all fields </div>
</div>
<div class="form-group">
<input class="form-control" type="text" />
<div class="form-message">Please all fields</div>
</div>
<div class="form-group">
<input class="form-control" type="text" />
<div class="form-message">Please all fields </div>
</div>
my Jquery
$(document).ready(function() {
$('.form-control').focus(function(e) {
if($(e.target).next('.form-message').css('display') == 'block'){
$('.form-message').hide();
}
});
});
Try it like this:
$(document).ready(function() {
$('.form-control').focus(function(e) {
var formMessage = $(e.target).next('.form-message');
if(formMessage.css('display') == 'block'){
formMessage.hide();
}
});
});
Fiddle: https://jsfiddle.net/gveukc0b/
Try this,
$(document).ready(function() {
$('.form-control').focus(function() {
$(this).next().hide();
});
});
I'm displaying a list of input fields, which looks like the code below. What I want to do is change the input to a TinyMCE window on click() or hover(). To do this I wrote the javascript below, but that isn't working obviously since they're not <textarea's.
HTML
<div class="col-md-12">
<div class="col-md-3">
<input id="first" class="input-field" value="test1"/>
</div>
<div class="col-md-3">
<input id="second" class="input-field" value="test2"/>
</div>
<div class="col-md-3">
<input id="third" class="input-field" value="test3"/>
</div>
<div class="col-md-3">
<input id="fourth" class="input-field" value="test4"/>
</div>
<!--This continues for some time-->
<div class="col-md-3">
<input id="fiftyseventh" class="input-field" value="test57"/>
</div>
<div class="col-md-3">
<input id="fiftyeight" class="input-field" value="test58"/>
</div>
<div class="col-md-3">
<input id="fiftyninth" class="input-field" value="test59"/>
</div>
<div class="col-md-3">
<input id="sixtieth" class="input-field" value="test60"/>
</div>
</div>
javascript
$("body").on("click", "input.input-field" function() {
var id = $(this).attr("id");
tinymce.EditorManager.execCommand("mceRemoveEditor", true, id);
tinymce.EditorManager.execCommand("mceAddEditor", true, id);
});
I thing issue is in your javascript code.
Please check this line-
$("body").on("click", "input.input-field" function() {
there is no "," (comma) after adding input.input-field. i should be there.
$("body").on("click", "input.input-field", function() {
and i have checked your all code is working fine.
Here is a DEMO