I have a problem with jQuery. I have an html page with a form.
In my form I have a select:
<select class="form-control" id="macro_area" name="macro_area">
<option value="0">Select type</option>
<option value="1">test</option>
<option value="7">Other</option>
</select>
When I select an option, I load a php file with this jQuery function:
$('document').ready(function() {
$('#macro_area').on('change', function() {
request = $.ajax({
type: "POST",
url: "get_bus_act_subcat.php",
data: { id_macro_area: id_macro_area },
dataType: "html"
});
request.done(function(response) {
$(".act_subcat").html (response);
});
});
The procedure works correctly and adds html code (generated from the file get_bus_act_subcat.php) to the page, in a div (<div class="form-group act_subcat">) originally empty.
Code generated and inserted in page is similar to this:
<div class="form-check">
<div class="container">
<div class="row">
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_5" value="5">Test1
</div>
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_6" value="6">Test2
</div>
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_2" value="2">Test3
</div>
......
</div>
</div>
</div>
So the final result is:
<div class="form-group act_subcat"> //this div is present on page when load
<div class="form-check">
<div class="container">
<div class="row">
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_5" value="5">Test1
</div>
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_6" value="6">Test2
</div>
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_2" value="2">Test3
</div>
......
</div>
</div>
</div>
</div>
Now I have my problem... I want to check with jQuery if any of the checkboxes have been checked but standard solution/selector not function.
I think because the checkbox fields have been added later.
Any ideas or suggestions?
Thanks
Maybe the following is helpful to you?
$('.act_subcat').on('change','input',
function(ev){console.log(this.id+" was clicked and is"+(this.checked?"":" not")+" checked");}
)
$('document').ready(function(){
$('#macro_area').on('change', function(){
$(".act_subcat").html(insertHTML(this.value));
});
});
// substitute for AJAX:
function insertHTML(i){return `<div class="form-check">
<div class="container">
<div class="row">
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_${i}" value="5">Test${i++}
</div>
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_${i}" value="6">Test${i++}
</div>
<div class="col-sm-3">
<input type="checkbox" class="form-check-input" name="id_sub_cat[]" id="sub_cat_${i}" value="2">Test${i}
</div>
</div>
</div>
</div>`;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" id="macro_area" name="macro_area">
<option value="0">Select type</option>
<option value="1">test</option>
<option value="7">Other</option>
</select>
<div class="act_subcat"></div>
I replaced your AJAX part with another HTML-importing function. I demonstrate the event binding with an .on relative to the .act_subcat container. The function inside will fire whenever an input element in this container changes. And this will work for any dynamically added content. Try it out.
One approach you can take is to attach the event listener to the returned content prior to adding it to the page:
$(response).find(':checkbox').on('change', function() {
console.log( `Checkbox with id: ${this.id} was clicked` );
});
//then ... append it ......html( response );
Related
I have multiple product forms on one page. Within each form are multiple color options (radio buttons) with either an available or unavailable attribute.
I would like to show a custom add to cart button for each product depending on if the selected color option is available, ie. if input[available]:checked show .button-available else show .button-unavailable
Expected results
When the page loads, the first color option is always pre-selected. Depending on its availability, the corresponding button should be shown. When the selection changes, the button should change if necessary.
HTML structure
<div class="list-item">
<form action="/cart/add" id="add-to-cart-1">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="1" class="red" value="1" available>
<input type="radio" name="color" id="2" class="blue" value="2" unavailable>
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>
<div class="list-item">
<form action="/cart/add" id="add-to-cart-2">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="3" class="red" value="3" unavailable>
<input type="radio" name="color" id="4" class="blue" value="4" available>
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>
My sad attempt at a script
$(document).ready(function() {
var $list = $('.list-item');
$list.each(function(){
if ( $('input[name=color][unavailable]:checked').val() !== 'true' ) {
$(this).closest('.button-available').show();
}
if ( $('input[name=color][available]:checked').val() !== 'true' ) {
$(this).closest('.button-unavailable').show();
}
$('input[name=color]:checked').on('change', function() {
if ($(this).attr('available') == 'true' ) {
$(this).closest('.button-available').show();
$(this).closest('.button-unavailable').hide();
}
else {
$(this).attr('unavailable') == 'true' ) {
$(this).closest('.button-available').show();
$(this).closest('.button-unavailable').show();
}
});
});
Thank you in advance for your guidance.
I propose to you to change your attribute available/unavailable by using data().
closest give you the more clause element with class. You also need nextAll or prevAll() to get next ou previous element.
I may forgot to fix something, tell me if something is missing.
var $list = $('.list-item');
$list.each(function(){
if ( $(this).find('input[name=color][data-statut="unavailable"]').is(':checked') === false ) {
$(this).find('.button-available').show();
}
if ( $(this).find('input[name=color][data-statut="available"]').is(':checked') === false ) {
$(this).find('.button-unavailable').show();
}
});
$('input[name=color]').on('change', function() {
if ($(this).data('statut') == 'available' ) {
$(this).closest('.radios').nextAll('.button-available').show();
$(this).closest('.radios').nextAll('.button-unavailable').hide();
}
if ($(this).data('statut') == 'unavailable' ) {
$(this).closest('.radios').nextAll('.button-available').hide();
$(this).closest('.radios').nextAll('.button-unavailable').show();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="list-item">
<form action="/cart/add" id="add-to-cart-1">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="1" class="red" value="1" data-statut="available">
<input type="radio" name="color" id="2" class="blue" value="2" data-statut="unavailable">
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>
<div class="list-item">
<form action="/cart/add" id="add-to-cart-2">
<div class="radios">
<div class="colors">
<input type="radio" name="color" id="3" class="red" value="3" data-statut="available">
<input type="radio" name="color" id="4" class="blue" value="4" data-statut="unavailable">
</div>
</div>
<div class="button-available" style="display:none;">
<input type="submit" value="Add to cart">
</div>
<div class="button-unavailable" style="display:none;">
<button>Out of stock</button>
</div>
</form>
</div>
I am developing a system I have been asked to do but the form elements are not clickable to enter any info in the fields, I have tried moving the form tag to above the very first div in the code below in case it was the issue but did not work unfortunately. I am not sure what else to try, can someone have a look at the code below please
Update: I have got the form elements working by adding zindex: 9999; to .form-group class in the CSS but now the datetimepicker is appearing behind the select dropdown menu. I have uploaded a screenshot of the issue to the link below
Here is a screenshot of my issue:
My code:
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-lg-12" id="detail">
<form name="addData" id="addData" action="" method="post">
<div class="row">
<div class="form-group col-lg-3">
<input type="hidden" name="eventID" id="eventID" class="form-control">
<label for="StartDate">Start Date: </label>
<input type="date" class="form-control" required name="StartDate" id="StartDate" />
</div>
<div class="form-group col-lg-3">
<label for="StartTime" style="margin: 0 0 15px 0;">Start Time: </label>
<div class='input-group date' id='datetimepicker3'>
<input name="StartTime" id="StartTime" type='text' required class="form-control" />
<span class="input-group-addon">
<i class="fa fa-clock-o"></i>
</span>
</div>
<script type="text/javascript">
$(function() {
$('#datetimepicker3').datetimepicker({
format: 'LT'
});
});
</script>
</div>
<div class="form-group col-lg-3">
<label for="StartDate">End Date: </label>
<input type="date" required class="form-control" name="EndDate" id="EndDate" />
</div>
<div class="form-group col-lg-3">
<label for="EndTime" style="margin: 0 0 15px 0;">End Time: </label>
<div class='input-group date' id='datetimepicker4'>
<input name="EndTime" id="EndTime" required type='text' class="form-control" />
<span class="input-group-addon">
<i class="fa fa-clock-o"></i>
</span>
</div>
<script type="text/javascript">
$(function() {
$('#datetimepicker4').datetimepicker({
format: 'LT'
});
});
</script>
</div>
</div>
<div class="row">
<div class="form-group col-lg-3">
<label for="riders_name">Riders Name: </label>
<select class="form-control" style="height: 34px;" required name="riders_name" id="riders_name"></select>
</div>
<div class="form-group col-lg-3">
<label for="horses_name">Horses Name : </label>
<select class="form-control" style="height: 34px;" required name="horses_name" id="horses_name">
<option value="">--Empty--</option>
</select>
</div>
<div class="form-group col-lg-3">
<label for="instructor_name">Instructor Name : </label>
<select class="form-control" style="height: 34px;" required name="instructor_name" id="instructor_name">
<option value="">--Empty--</option>
</select>
</div>
<div class="form-group col-lg-3">
<label for="groom_name">Groom Name : </label>
<select class="form-control" style="height: 34px;" required name="groom_name" id="groom_name">
<option value="">--Empty--</option>
</select>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-lg-9">
<label for="comments">Comments : </label>
<textarea name="comments" id="comments" class="form-control"></textarea>
</div>
<div class="form-group col-lg-3">
<label for="Repeat">Repeat : </label>
<select class="form-control" style="height: 34px;" required name="Repeat" id="Repeat">
<option value="0">none</option>
<option value="1">Daily</option>
<option value="2">Weekly</option>
<option value="3">Monthly</option>
</select>
</div>
</div>
<div class="row">
<div class="form-group col-lg-1">
<button type="submit" class="btn btn-primary" name="submit" id="submit">Submit</button>
</div>
<div class="form-group col-lg-1">
<button type="submit" class="btn btn-danger" name="cancel" id="cancel">Cancel</button>
</div>
<div class="form-group col-lg-5">
</div>
<div class="form-group col-lg-5">
</div>
</div>
</form>
</div>
<script>
$.getJSON("fullcalendar/getriders.php", function(data) {
var select = $('#riders_name'); //combo/select/dropdown list
if (select.prop) {
var options = select.prop('options');
} else {
var options = select.attr('options');
}
$('option', select).remove();
$.each(data, function(key, value) {
options[options.length] = new Option(value['name'], value['id']);
});
});
$.getJSON("fullcalendar/getinstructors.php", function(data) {
var select = $('#instructor_name'); //combo/select/dropdown list
if (select.prop) {
var options = select.prop('options');
} else {
var options = select.attr('options');
}
$('option', select).remove();
$.each(data, function(key, value) {
options[options.length] = new Option(value['name'], value['id']);
});
});
$.getJSON("fullcalendar/getgrooms.php", function(data) {
var select = $('#groom_name'); //combo/select/dropdown list
if (select.prop) {
var options = select.prop('options');
} else {
var options = select.attr('options');
}
$('option', select).remove();
$.each(data, function(key, value) {
options[options.length] = new Option(value['name'], value['id']);
});
});
</script>
I see several empty <select></select> tags in your form. I suspect you are intending text fields there? If so you need to replace <select></select> with <input type="text" />.
Example, this:
<label for="riders_name">Riders Name: </label>
<select class="form-control" style="height: 34px;" required name="riders_name" id="riders_name"></select>
...should be:
<label for="riders_name">Riders Name: </label>
<input class="form-control" type="text" style="height: 34px;" required name="riders_name" id="riders_name" />
Select drop down always higher z-index set by default by browsers. You have two row one underneath other. You should in top row set higher z-index value with position: relative; then I hope it will work as expected.
Add CSS code with following way:
.higher-z-index {
postion: relative; // This line help z-index work relatively
z-index: 999;
}
Your markup will be something like that:
<div class="row higher-z-index">
.....
....
</div>
<div class="row">
.....
....
</div>
Why this approach:
You should keep it mind this is dose not matter how much z-index value applied in .form-group selector it will work for only sibling elements. But in your case it is going behind of next row elements because modern browsers set by default higher z-index in each next sibling elements until we set explicitly. So underneath row getting higher z-index than top row. So it is dose not matter how many higher z-index value are applied inside of top row container, all will go behind of next row container.
I have an issue where by I need a nicer-looking way to use select option that caters to Countries (240+ countries). I plan to use select2 to enhance the searching experience.
Now, a regular select-option with some values in it will just look like this
<select>
<option value=..></option>
<select>
But in the case of selecting Nationality, there are 240+ countries and suddenly a nice piece of code like this will look terrible.
<div class="form-group">
<label for="alias" class="col-sm-2 control-label">Alias</label>
<div class="col-sm-10">
<input type="text" class="form-control input-sm" id="alias" placeholder="Employee alias">
</div>
</div>
<div class="form-group">
<label for="dob" class="col-sm-2 control-label">DoB</label>
<div class="col-sm-10">
<input type="text" class="form-control input-sm" id="dob" placeholder="Date of birth">
</div>
</div>
<! 240++ lines worth of options->
Any inputs?
You can use JQuery and Array() for this kind of things someway. Just put the countries you want to be shown in var countries which is array. But it will be easier to make .cvs file that has all the country names on the database and call them from it. Anyways this JQuery source might help for your question.
$(document).ready(function(){
var countries = new Array();
/* here you put all the countries you want in array */
countries = ["Korea", "USA", "China", "India", "etc"];
for(i = 0;i < countries.length;i++) {
$("#select2").append("<option value='"+countries[i]+"'>"+countries[i]+"</option>");
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>test</title>
</head>
<div class="form-group">
<label for="alias" class="col-sm-2 control-label">Alias</label>
<div class="col-sm-10">
<input type="text" class="form-control input-sm" id="alias" placeholder="Employee alias">
</div>
</div>
<div class="form-group">
<label for="dob" class="col-sm-2 control-label">DoB</label>
<div class="col-sm-10">
<input type="text" class="form-control input-sm" id="dob" placeholder="Date of birth">
</div>
</div>
<!-- 240++ lines worth of options -->
<div class="form-group">
<label for="select2" class="col-sm-2 control-label">Select Country</label>
<div class="col-sm-10">
<!-- this is where countries enters -->
<select name="" id="select2">
</select>
</div>
</div>
</body>
</html>
Hello I'm trying to build a HTML previewer: while editing HTML source in a textarea I want it to be previewd inside a container div:
Everything is working exept that in the browser console i get the following two type of errors:
TypeError: bindings.push is not a function
While writing new code in the text area I get a html validation error, I believe it comes from the ngSanitize module
How can I fix the first problem?
How can I catch the second error and display it out to the interface in order to alert the Editor?
Thanks for help.
This is made under a blade view (LRVL5), here's the code:
#extends('app')
#section('content')
<script>
var bindings = new Array();
'use strict';
angular.module('htmlEditor', ['ngSanitize'])
.controller('HTMLController', function() {
this.html = '{!! preg_replace("/\'/", "\\'", $html) !!}';
});
</script>
<div ng-app="htmlEditor" ng-controller="HTMLController as htmlctrl">
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<h3>Edit</h3>
</div>
<div class="col-md-6">
<h3>Preview</h3>
</div>
<div class="col-md-6">
<form>
<div class="form-group">
<label for="elementId">Id</label>
<input type="text" class="form-control" id="elementId" value="{{$id}}" >
</div>
<div class="form-group">
<label for="elementName">Name</label>
<input type="text" class="form-control" id="elementName" value="{{$name}}">
</div>
<div class="form-group">
<label for="elementHTML">HTML Body</label>
<textarea ng-model="htmlctrl.html" rows="20" class="form-control" id="elementHTML" placeholder="Email Html source for Body">{{$html}}</textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<form>
</div>
<div class="col-md-6">
<div class="jumbotron" ng-bind-html="htmlctrl.html" >
</div>
</div>
</div>
</div>
</div>
#endsection
Using bootstrap and can not set an input to hidden. It can be easily set to hidden with JS using
document.getElementById("inputCommentsBrand").style.visibility = "hidden";
but I would like it to be hidden by default.
<div class="form-group">
<label for="inputComments" class="col-sm-2 control-label">Markings</label>
<div class="col-sm-4">
<div><input hidden type="text" class="form-control" id="inputCommentsBrand" name="inputFlags[]" placeholder="Brand and Location: Sbar RH"></div>
<div><input type="text" class="form-control" id="inputCommentsEarTag" name="inputFlags[]" placeholder="Ear Tag Color & #: Green 165"></input></div>
</div>
</div>
Use <body onload="myFunction()"> to hide the field.
Below is the running example.
<script>
function myFunction() {
document.getElementById("inputCommentsBrand").style.visibility = "hidden";
}
</script>
<html>
<body onload="myFunction()">
<div class="form-group">
<label for="inputComments" class="col-sm-2 control-label">Markings</label>
<div class="col-sm-4">
<div><input type="text" class="form-control" id="inputCommentsBrand" name="inputFlags[]" placeholder="Brand and Location: Sbar RH"></div>
<div><input type="text" class="form-control" id="inputCommentsEarTag" name="inputFlags[]" placeholder="Ear Tag Color & #: Green 165"></input></div>
</div>
</div>
</body>
</html>
Instead of visibility:hidden
Use display:none