I'm having some issues with the code that i wrote up to add and remove fields to a form. I currently have a jquery script that is working on another group of fields that require the same functionality, but for some reason this second Jquery i have set up is only adding the fields and not removing them here is the code.
HTML:
<label for="EmergeContactType">Emergency Contacts</label>
<hr>
<div class="addcon">
<p>
<label for="EmergeContactType">Affilation</label>
<select name="properties[EmergContactType]">
<option value="1">Primary</option>
<option value="2">Secondary</option>
<option value="3">Doctor</option>
<option value="4">Aunt</option>
<option value="5">Uncle</option>
<option value="1">Babysitter</option>
<option value="2">Caregiver</option>
<option value="3">Grandmother</option>
<option value="4">Grandfather</option>
<option value="5">Step-mother</option>
<option value="5">Step-father</option>
</select><br>
<label for="EmergeContactType">Name</label><br>
<input type="text" size="20" class="emerg" name="properties[EmergencyName]" align="right" /><br>
<label for="EmergeContactType">Number</label><br>
<input type="text" width="15" maxlength="15" class="emerg" name="properties[EmergContactNum]" pattern="[789][0-9]{9}" align="right"/><br>
</p>
</div>
Add Contact
Script:
$(function() {
var plusDiv = $('#addcon');
var y = $('#addcon p').size() + 1;
$('#addContact').on('click', function() {
$('<p>'+
'<label for="EmergeContactType">Affilation</label>'+
'<select name="properties[EmergContactType'+ y +']">'+
'<option value="1">Primary</option>'+
'<option value="2">Secondary</option>'+
'<option value="3">Doctor</option>'+
'<option value="4">Aunt</option>'+
'<option value="5">Uncle</option>'+
'<option value="1">Babysitter</option>'+
'<option value="2">Caregiver</option>'+
'<option value="3">Grandmother</option>'+
'<option value="4">Grandfather</option>'+
'<option value="5">Step-mother</option>'+
'<option value="5">Step-father</option>'+
'</select><br>'+
'<label for="EmergeContactType">Name</label><br>'+
'<input type="text" id="EmergencyName" class="textbox" name="properties[EmergencyName'+ y +']" /><br>'+
'<label for="EmergeContactType">Number</label><br>'+
'<input type="text" id="EmergContactNum" class="textbox" name="properties[EmergContactNum'+ y +']" /><br>'+
'Remove Contact</p>').appendTo(plusDiv);
y++;
return false;
});
plusDiv.on('click', '.remCon', function() {
if( i > 2 ) {
$(this).parents('p').remove();
y--;
}
return false;
});
});
JFiddle
http://jsfiddle.net/4SVXt/23/
Thanks in advance
It's because you're calling to remove the parent p of #addcon. You don't have an element with that id, hence no parent p so nothing gets removed in this statement: $(this).parents('p').remove();
EDIT :
UPDATED EXAMPLE
I felt like your code was a little heavy so I made some changes that should make this more efficient. Instead of rewriting the form in the code we just clone() it, then append the form to the div containing the forms. We then assign a remove button to each form that will remove its own form. The code is shorter and much more portable.
$('.addContact').click(function(e) {
e.preventDefault();
var clonedForm = $('form[name="add_contact"]:first').clone(); // clone the form
$('.addcon').append(clonedForm); // append the form to the div
var removeLink = 'Remove Contact'; // create a remove link
$('form[name="add_contact"]:last').append(removeLink);
});
$('.addcon').on('click', '.removeContact', function(e) { // use on() to delegate
e.preventDefault();
$(this).closest('form').remove();
});
References:clone() append() on()
Related
What I am trying to figure out is how to have an html input field appear when the value of other is selected from a dropdown menu. Right now the values for the dropdown list are coming from the results of a MySQL DB query, which works, but I can not seem to figure out how to get an input to appear when I select the other option.
$query = mysql_query("SELECT type FROM Dropdown_Service_Type"); // Run your query
echo '<select name="service_type">'; // Open your drop down box
echo '<option value="NULL"></option>';
// Loop through the query results, outputing the options one by one
while ($row = mysql_fetch_array($query)) {
echo '<option value="'.$row['type'].'">'.$row['type'].'</option>';
}
echo '<option value="Other">Other</option>';
echo '</select>';// Close your drop down box
Use javascript, like in the example below. We can add an input field and have it hidden by default, using the style attribute:
<input name='otherInput' id='otherInput' type="text" style="display: none" />
var otherInput;
function checkOptions(select) {
otherInput = document.getElementById('otherInput');
if (select.options[select.selectedIndex].value == "Other") {
otherInput.style.display = 'block';
}
else {
otherInput.style.display = 'none';
}
}
<select onchange="checkOptions(this)" name="service_type" id="service_type">
<option value="NULL"></option>
<option value="43">43</option>
<!-- other options from your database query results displayed here -->
<option value="Other">Other</option>
</select>
<!-- the style attribute here has display none initially, so it will be hidden by default -->
<input name='otherInput' id='otherInput' type="text" style="display: none" />
There are 3rd party libraries like jQuery, AngularJS, PrototypeJS, etc., which can be used to make the code simpler by adding shortcut methods for DOM manipulation (though you should read this post). For example, with jQuery, using .on() (for the event handler binding), .show() and .hide() for the input display toggling, etc:
var otherInput;
var serviceTypeInput = $('#service_type');
serviceTypeInput.on('change', function() {
otherInput = $('#otherInput');
if (serviceTypeInput.val() == "Other") {
otherInput.show();
} else {
otherInput.hide();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="service_type" id="service_type">
<option value="NULL"></option>
<option value="43">43</option>
<option value="Other">Other</option>
</select>
<input name='otherInput' id='otherInput' type="text" style="display: none" />
$(function() {
$('#sample').change(function() {
var val = this.value; // get the value of the select.
if (val == 'other') { // if the value is equal to "other" then append input below the select
$('html').append('<input type="text" id="inputOther"/>');
} else { // else then remove the input
$('#inputOther').remove();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="sample">
<option value="test1">test1</option>
<option value="test2">test2</option>
<option value="test3">test3</option>
<option value="other">other</option>
</select>
it only pick first value from dynamic checkbox, when i click on check box i want to assign dynamic checkbox value in dropdown list
$(document).ready(function() {
$('input[type="checkbox"]').click(function() {
if ($(this).is(":checked")) {
//var val = $("#country option:selected").val();
var val = $("#c1").val();
$("#country").val(val);
alert(val);
} else if ($(this).is(":not(:checked)")) {
alert("Checkbox is unchecked.");
}
});
});
<select name="cat_id1" id="country" onChange="ajaxFunction()">
<option value=0>Show All</option>
<?php
foreach ($results as $result) {
?>
<option value="<?php echo $result[mem_ID];?>"><?php echo $result[mem_email];?></option>
<?php
}
?>
</select>
<td>
<input type="checkbox" name="name[]" id="c1" value="<?php echo $result['mem_email']; ?>">
</td>
Ok, after I've checked the code presented by OP:
id attribute should always be unique on the page. You can not add id="c1" for 3 chekboxes, only for one.
Instead of using id use a class selector.
<input type="checkbox" name="name[]" id="someUniqeu" class="selectTrigger" value="<?php echo $result['mem_email']; ?>">
Then use it in your jQuery:
$('.selectTrigger').click(function() {
if ($(this).is(":checked")) {
("#country").val($(this).val());
I'm using .append() to create new lines inside a form composed by select boxes that are updated based on the value of the first one.
The purpose is to create as many lines (groups of select boxes) as needed and by changing the value of the first select box they update the others in the line.
I managed to do this with the following code:
HTML:
<div class="input_fields_wrap">
<table>
<tr>
<td class="span1">Select_1: </td>
<td class="span1">Select_2: </td>
<td class="span1">Select_3: </td>
<td class="span1">Select_4: </td>
</tr>
<tr>
<td>
<select class="span2" id='Select_1' name='Select_1[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="span2" id='Select_2' name='Select_2[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="span2" id='Select_3' name='Select_3[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
<td>
<select class="span2" id='Select_4' name='Select_4[]'>
<option value="0">Seleccione...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>
</table>
</div>
JQUERY:
<script type="text/javascript">
$('#Select_1').change(function(){
$('#Select_2').load("file.php?ID1=" + $(this).val());
$('#Select_3').load("file.php?ID2=" + $(this).val());
$('#Select_4').load("file.php?ID3=" + $(this).val());
});
var max_fields = 20;
var x = 1;
var wrapper = $(".input_fields_wrap");
var add_button = $(".add_field_button");
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++;
var html = '<div class="input_fields_wrap">';
html = html + '<select class="span2" id="Select_1" name="Select_1[]"><option value="0">Seleccione...</option><?php foreach($var1 as $row1){echo '<option value="'.$row1['ID'].'">'.$row1['Name'].'</option>';}?></select>';
html = html + '<select class="span2" id="Select_2" name="Select_2[]"><option value="0">Seleccione...</option><?php foreach($var2 as $row2){echo '<option value="'.$row2['ID'].'">'.$row2['Name'].'</option>';}?></select>';
html = html + '<select class="span2" id="Select_3" name="Select_3[]"><option value="0">Seleccione...</option><?php foreach($var3 as $row3){echo '<option value="'.$row3['ID'].'">'.$row3['Name'].'</option>';}?></select>';
html = html + '<select class="span2" id="Select_4" name="Select_4[]"><option value="0">Seleccione...</option><?php foreach($var4 as $row4){echo '<option value="'.$row4['ID'].'">'.$row4['Name'].'</option>';}?></select>';
html = html + '<button onclick="#" class="remove_field">Remove</button></div>';
$(wrapper).append(html);
}
});
$(wrapper).on("click",".remove_field", function(e){
e.preventDefault();
$(this).parent('div').remove();
x--;
});
</script>
This all works ok for the first line.
All the others that are created by the .append() in Jquery arenĀ“t updated when the select box "Select_1" value is changed.
I need that all the lines created by append have the funcionality to update the 3 select boxes by change the first select box value.
I'm using JQuery 2.1.4
Thanks in advance.
Just found the problem.
It was a noob question after all.
Just have to:
Call the autocomplete function again to resolve the combobox problem;
Rename the comboboxes in the append code with a autoincrement number to contemplate each line;
Run the update comboboxes code after the append with the new comboboxes name;
Here's the code to put right after the append command:
$(wrapper).append(html);
$("#Select"+x).combobox({
select: function (event, ui){
$('#Select'+x).load("file.php?ID1=" + $(this).val());
$('#Select'+x).load("file.php?ID2=" + $(this).val());
$('#Select'+x).load("file.php?ID3=" + $(this).val());
}
});
The solution was found with some help found here: jQuery append elements not working with autocomplete
Thanks for all!
I have select with this options:
<select name="kategory" class="select-field">
<option disabled>ATRACTIONS
<option value="">
<option value="Castles">Castles
<option value="History">History
</select>
And I have a chceck box:
Do you want to eat?<input type="checkbox" class="checkbox" name="restaurants" value="" />
after I click in chceckbox for true I need change the select option values to :
<option disabled>Restaurants
<option value="China food">Chinas food
<option value="Pizza">Pizza
<option value="Pub">Pub
but with no need to refresh page. How can I do that? thanks
I would use javascript or jquery whatever you are more comfortable with. Haven't tried this but something like this should work.
HTML:
<select name="kategory" class="select-field">
<option disabled>ATRACTIONS
<option value="">
<option value="Castles">Castles
<option value="History">History
</select>
<br>
<span>Do you want to eat?</span>
<input type="checkbox" class="checkbox" name="restaurants" value="" onchange="changeSelect()"/>
JQUERY:
// options
var myOptions = {
val1 : 'Chinese food',
val2 : 'Pizza',
val3 : 'Pub'
};
var mySelect = $('.select-field');
function changeSelect(element){
if (element.checked){
$.each(myOptions, function(val, text) {
mySelect.append(
$('<option></option>').val(val).html(text)
);
});
} else {
return;
}
};
If you do not want to refresh the page, you will want to look into using perhaps Javascript/jQuery. These are great tools for what you are looking for (IMO).
You will be able to reference your dropdown 'kategory' and update the options after checking the checkbox.
If that's the route you would like to go and are not sure how to do so, let us know and we can include examples.
Edit: unsalted has the right idea using objects. If you want another option that just empties the select and builds the html directly here is another way using jQuery.
$('input[name="restaurants"]').change(function(){
if( this.checked )
{
var select = $('select[name="kategory"]');
select.empty();
var options = '';
options += '<option disabled>Restaurants</option>';
options += '<option value="China food">Chinas food</option>';
options += '<option value="Pizza">Pizza</option>';
options += '<option value="Pub">Pub</option>';
select.html(options);
}
});
I have the following html code:
Region:<span id="region">Rajkot</span><br>
Activity:<span id="Select">Cricket</span><br>
Radius:<input type="text" id="radius" value="50000"></input><br>
Gender:<select name="Gender" id="Sex">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Both</option>
</select><br>
Date:<input type="text" id="date" value="2013:04:03 11:54:00"></input><br>
<input type="button" value="Go">
I want to store all the values in array on button click.
I want to have the following functionality:
When I click on go button, all values of html are store in array.
Put everythign inside form ( I assume you already have one)
<form>
Region:<span id="region">Rajkot</span><br>
Activity:<span id="Select">Cricket</span><br>
Radius:<input type="text" id="radius" value="50000"></input><br>
Gender:<select name="Gender" id="Sex">
<option value="1">Male</option>
<option value="2">Female</option>
<option value="3">Both</option>
</select><br>
Date:<input type="text" id="date" value="2013:04:03 11:54:00"></input><br>
<input type="button" value="Go">
</form>
$('form').serializeArray(); will do the trick for you.
Reference
Use serializeArray()
$('form').submit(function() {
$('form').serializeArray();
return true;
});
Simple way to do it using your structure:
FIDDLE
//javascript
function gatherData(){
var arrData = [];
arrData['region'] = $('#region').html();
arrData['Select'] = $('#Select').html();
arrData['radius'] = $('#radius').val();
arrData['Gender'] = $('#Sex').val();
arrData['date'] = $('#date').val();
return arrData;
}
$(document).ready(function(){
//add Id or class to the button for better selector
$('input[type=button]').click(function(){
var data = gatherData();
console.log(data);
//alert(data);
});
});
consider using form as other peple sugested.
<?php
session_start();
if(!isset($_POST["submit"])){
$_SESSION["abc"]=array("C", "C++","JAVA","C#","PHP");
}
if(isset($_POST["submit"])){
$aa=$_POST['text1'];
array_push( $_SESSION["abc"],$aa);
echo "hello";
foreach( $_SESSION["abc"] as $key=>$val)
{
echo $val;
}
}
?>