I see lots of discussion about setting and getting a field value via jQuery. But my issue is that I am unable to get a value after I submit, then edit the value in an input field, then submit again. So the first time it works, the second time it doesn't.
Here's my scenario: I have a form that is used to manage filters for a dataset:
<form id="filtersForm">
<div class="row">
<div class="col-md-12">
<div id="addContactFilter">
<div class="row filterRow" id="contact_filter_780b902d">
<div class="col-md-4">
<!-- Selectize dropdown of fields to query by -->
<div class="control-group">
<select id="filterField_780b902d" class="selectizeField selectized" name="filterField_780b902d" placeholder="Select field..." tabindex="-1" style="display: none;">
<option value="firstName" selected="selected">First Name</option>
</select>
<div class="selectize-control selectizeField single">
<div class="selectize-input items has-options full has-items">
<div data-value="firstName" class="item">First Name</div>
<input type="text" autocomplete="off" tabindex="" style="width: 4px; opacity: 0; position: absolute; left: -10000px;">
</div>
<div class="selectize-dropdown single selectizeField" style="display: none; visibility: visible; width: 248px; top: 34px; left: 0px;">
<div class="selectize-dropdown-content">
<div data-value="firstName" data-selectable="" class="option selected">First Name</div>
<div data-value="lastName" data-selectable="" class="option">Last Name</div>
<div data-value="organization" data-selectable="" class="option">Organization</div>
<div data-value="jobTitle" data-selectable="" class="option">Job Title</div>
<div data-value="city" data-selectable="" class="option">City</div>
<div data-value="state" data-selectable="" class="option">State</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<select id="filterOperator_780b902d" class="form-control">
<option value="=">equals</option>
<option value="contains">contains</option>
</select>
</div>
<div class="col-md-4">
<div id="contact_filter_value_780b902d">
<!-- THIS IS THE FIELD IN QUESTION -->
<input class="form-control" type="text" name="filterValue_780b902d" id="filterValue_780b902d" data-type="String" placeholder="Input Value">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row filterCriteria">
<div class="col-md-12">
<input class="btn btn-default" type="submit" value="Submit">
</div>
</div>
</form>
When I first submit the form, I am able to successfully get the value for the input field with id #filterValue_780b902d. But if the user edits the value in the form, then submits again, the original value is returned.
Here's how I try to retrieve the value (I am using Meteor JS, which provides an event map on the click event with an event parameter):
var target = event.target,
fieldName = 'filterValue_' + fieldId;
thisVal1 = target[fieldName].value,
thisVal2 = template.find( "[name='" + fieldName + "']" ).value,
thisVal3 = $( '#' + fieldName ).attr('value'),
thisVal4 = $( '#' + fieldName ).val();
When I submit the form the first time with the input value "Meghan", here's the response logs:
contactFilters.filtersForm submit
contactFilters.filtersForm.fieldId: 780b902d
contactFilters.filtersForm.fieldName: filterValue_780b902d
contactFilters.filtersForm.thisVal1: Meghan
contactFilters.filtersForm.thisVal2: Meghan
contactFilters.filtersForm.thisVal3: undefined
contactFilters.filtersForm.thisVal4: Meghan
But when I change the input value to "Karen", here's what I get:
contactFilters.filtersForm submit
contactFilters.filtersForm.fieldId: 780b902d
contactFilters.filtersForm.fieldName: filterValue_780b902d
contactFilters.filtersForm.thisVal1: Meghan
contactFilters.filtersForm.thisVal2: undefined
contactFilters.filtersForm.thisVal3: undefined
contactFilters.filtersForm.thisVal4: undefined
So three questions:
Why does thisVal1 not update when the text of the field is updated?
Why does thisVal2 and thisVal4 become undefined?
What's the proper way to get the field value that will update when the input field value updates?
Thanks in advance for your help!
i believe the correct way to get value in jQuery is to use the val() function.
so you should probably be using:
var target = event.target,
fieldName = 'filterValue_' + fieldId;
myValue = $( '#' + fieldName ).val();
as stated here:
https://stackoverflow.com/a/15903284/6368401
and here:
http://api.jquery.com/val/
in javascript
document.getElementById("input_id").value
other javascript methods explained here:
How do I get the value of text input field using JavaScript?
to answer your 'undefined' questions:
var target = event.target,
fieldName = 'filterValue_' + fieldId;
thisVal1 = target[fieldName].value,
this is getting the original value from the postback.
thisVal2 = template.find( "[name='" + fieldName + "']" ).value,
i cant answer this one, as i dont know the answer
thisVal3 = $( '#' + fieldName ).attr('value');
the attribute "value" was never defined in the markup.
Related
I have two forms. form1's content is a choice option filled by name, matricule, and salary, and a button validate, which hides form1 and displays form2. I want when I click on the button, the contents of select to separate and show like this:
name:
registration number :
salary :
$(document).ready(function() {
$("#hide").click(function() {
let name1 = $('#name').text();
let matricule1 = $('#matricule').text();
let salary1 = $('#salary').text();
$('#nam').text(name1);
$('#mat').text(matricule1);
$('#sal').text(salary1);
$("#form1").hide();
$("#form2").show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="form1">
<div class="form-group col-md-3">
<select class="form-control">
<option>
<div id="name">imad</div>
<div id="matricule">22</div>
<div id="salary">6000</div>
</option>
</select>
</div>
<div class="form-group col-md-offset-5 ">
<button class="btn btn-success " id="hide">valider</button>
</div>
</div>
<!--form 2-->
<div id="form2">
<h4>name : <span id="nam"></span></h4>
<h4>matricule : <span id="mat"></span></h4>
<h4>salary : <span id="sal"></span></h4>
</div>
As Rory said, you cannot have HTML within an option element, this is invalid. Use a custom drop-down select library (some examples of such are here: https://www.sitepoint.com/13-jquery-selectboxdrop-down-plugins/)
Now, if you know the format of the data you are using to populate the select option values with (you are getting data from server side, etc.), you could then format the data on the client side with some sort of separator to create a chained together option value for the 3 different fields, like I've done in the example below using a dash (-). You could then split the string value of the option selected, and parse it out to individual text fields. Again, this is all dependent on how you get and format the data within the select element.
$(document).ready(function() {
$("#hide").click(function() {
let selectOption = $('.form-control').val();
let optionSplits = selectOption.split('-');
let name1 = optionSplits[0];
let matricule1 = optionSplits[1];
let salary1 = optionSplits[2];
$('#nam').text(name1);
$('#mat').text(matricule1);
$('#sal').text(salary1);
$("#form1").hide();
$("#form2").show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="form1">
<div class="form-group col-md-3">
<select class="form-control">
<option value="imad-22-6000">imad 22 6000</option>
<option value="gupta-24-8000">gupta 24 8000</option>
<option value="ganesh-27-5000">ganesh 27 5000</option>
</select>
</div>
<div class="form-group col-md-offset-5 ">
<button class="btn btn-success " id="hide">valider</button>
</div>
</div>
<!--form 2-->
<div id="form2">
<h4>name : <span id="nam"></span></h4>
<h4>matricule : <span id="mat"></span></h4>
<h4>salary : <span id="sal"></span></h4>
</div>
I am trying to fill a textbox when a value is selected from a dropdown list.
I am able to do that, but in the process of doing so, I am unable to pick the value appearing in the textbox in my python script.
Now in my database I am getting a none value for the UserID. And the actual user ID is appearing in the Name column, as you can see in the screenshot.
This is the HTML:
<div class="col-xs-6 col-sm-6 col-md-6 col-lg-6" style="margin:0px 0px 0px 50px"><label for="form_lastname" class="control-label">Name</label>
<select name="username" id="username" required class="form-control">
<option value=""></option>
<option value="maahir10">Dadlani, Maahir</option>
</select>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="width:145px
<label for="form_lastname" class="control-label">User ID</label>
<textarea type="text" id="userid" style="height:35px;width:120px;font-family:Raleway" disabled="yes" required></textarea>
</div>
The JS:
<script type="text/javascript">
var mytextbox = document.getElementById('userid');
var mydropdown = document.getElementById('username');
mydropdown.onchange = function(){
mytextbox.innerHTML = this.value;
}
</script>
So, how do i fix this error and get both the values in my DB. Please advise. Thank you.
You should use .value option for input field not .innerHTML
<script type="text/javascript">
var mytextbox = document.getElementById('userid');
var mydropdown = document.getElementById('username');
mydropdown.onchange = function(){
mytextbox.value = this.value;
}
</script>
also you used disabled on your textarea, and that prevents the value to be sent via POST, use readonly instead.
I want to populate the value of the "eventTitle" in "Requirement" input box when some one click on the corresponding check box. i.e If some one clieck on the check box of Vels Group Of Instutions then automatically i want this to populate in texbox with name "Requirement" if multiple check box are clicked i want it to be comma seperated. Below is the code i tried to get but it is not working and getting undefined.
<div class="wid100">
<div class="eventTitle">Vels Group Of Instutions</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">This is world wide institute of technology </div>
<div class="selectEvent">
<input type="checkbox" class="seminar selected" id="179">
<label for="179"></label>
</div>
</div>
<div class="wid100">
<div class="eventTitle">Title goes here</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">sdfdsafasdfdsafdsafsadfsdfsdf </div>
<div class="selectEvent">
<input type="checkbox" class="seminar" id="179">
<label for="179"></label>
</div>
</div>
<input type="text" name="Requirement" placeholder="Title 01" id="divclass" required="required" class="pull-left" />
<script type="text/javascript" src="js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
$(".seminar").click(function () {
if ($(this).is(":checked")) {
//checked
$(this).addClass("selected");
var event_title = "";
event_title = $(".selected").siblings('.eventTitle').val();
console.log(event_title); return false;
} else {
//unchecked
$(this).removeClass("selected");
}
});
.eventTitle is not the sibling of .selected and the .eventTitle is a div element having no value, text there. change this line
event_title = $(".selected").siblings('.eventTitle').val();
to
event_title = $(this).parent().siblings('.eventTitle').text();
or
event_title = $(this).parent().siblings('.eventTitle').html();
The issue you have is because .eventTitle is not a sibling of the clicked checkbox, so the DOM traversal logic is wrong. div elements also do not have a val(), so you should use text() or html() instead.
However, you can improve the logic and also achieve the comma separated list of the selected event titles by using map() to build an array which you can then join() before setting in the value of #divclass. Try this:
$(".seminar").click(function() {
$(this).toggleClass('selected', this.checked);
var eventNames = $('.seminar:checked').map(function() {
return $(this).closest('.wid100').find('.eventTitle').text();
}).get().join(',');
$('#divclass').val(eventNames);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="wid100">
<div class="eventTitle">Vels Group Of Instutions</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">This is world wide institute of technology </div>
<div class="selectEvent">
<input type="checkbox" class="seminar selected" id="179">
<label for="179"></label>
</div>
</div>
<div class="wid100">
<div class="eventTitle">Title goes here</div>
<div class="eventDate">2017-07-25</div>
<div class="eventVenue">sdfdsafasdfdsafdsafsadfsdfsdf </div>
<div class="selectEvent">
<input type="checkbox" class="seminar" id="179">
<label for="179"></label>
</div>
</div>
<input type="text" name="Requirement" placeholder="Title 01" id="divclass" required="required" class="pull-left" size="100" />
I'd suggest changing the id of the #divclass to something more descriptive, as the element is not a div, and it's an identifier, not a class.
Finally, your .seminar elements have the same id attribute which is invalid. You should ensure that the ids are unique within the DOM - assuming that this is not just a typo from copy/pasting the code in the question.
I have following code
<div class="form-group">
<select class="form-control" name="persons">
<option value="1">{{Lang::get('user')}}</option>
<option value="0">{{Lang::get('admin')}}</option>
</select>
</div>
I want something like this if is selected the option with value = 1 to show an input like this
<div class="form-group">
{{ Form::text('name','',['required', 'class'=>'form-control', 'placeholder'=>Lang::get('user_name')]) }}
</div>
else if the value is 0
<div class="form-group">
{{ Form::text('admin','',['required', 'class'=>'form-control', 'placeholder'=>Lang::get('admin_name')]) }}
</div>
I'm new in Laravel and I don't know how to show inputs by value.
acually this is DOM issues
$('#persons').on('change', function(){
var value = $(this).val();
$('.form').find('div').addClass('hide');
if(value === 1){
$('.form').find('.one').remove('hide')
$('.form').find('.one').addClass('show')
}else{
$('.form').find('.two').remove('hide')
$('.form').find('.two').addClass('show')
}
});
enter code here
i made example . refer i update code
https://jsfiddle.net/channasmcs/5fmmLqax/
thanks
refre on change JQ
jQuery select change show/hide div event
Update
Tidied up the solution in progress and added some extra details
I have a form area which creates clones based on a template. In order to make sure the form transmits in an order, the script goes through the form at send time appending a number which defines the current batch set. Below is an over simplified representation of what is going on:
<form>
<div class="batch-template">
<div class="batch-piece">
<a class="clone" />
<input name="test-input">
<input name="another-test-input">
<select name="a-drop-down">
</div>
</div>
<div class="batch-paste-area">
</div>
</form>
When the page starts:
The contents of "batch-template" are stored to an object variable
The original template is removed from the page
An instance of the template is appended to the "batch-paste-area"
The following is an example of the output created after clicking twice.
<form>
<div class="batch-template">
</div>
<div class="batch-paste-area">
<div class="batch-piece">
<a class="clone" />
<input name="test-input">
<input name="another-test-input">
<select name="a-drop-down">
</div>
<div class="batch-piece">
<a class="clone" />
<input name="test-input">
<input name="another-test-input">
<select name="a-drop-down">
</div>
</div>
</form>
When it comes to submitting the form: prior to serialization, I would like the script to loop through each "batch-piece" within "batch-paste-area" and add a count value to the end of each form field name. Continuing with the set above, the result (to a browser) would seem like that shown below:
<form>
<div class="batch-template">
</div>
<div class="batch-paste-area">
<div class="batch-piece">
<a class="clone" />
<input name="test-input1">
<input name="another-test-input1">
<select name="a-drop-down1">
</div>
<div class="batch-piece">
<a class="clone" />
<input name="test-input2">
<input name="another-test-input2">
<select name="a-drop-down2">
</div>
</div>
</form>
So far, I can either loop through EVERY input within the paste area or just select the last.
Selecting the last batch-piece is simple:
var intCount = 1;
$('.batch-paste-area .batch-piece').each(function(){
/*
* Would like to be able to loop through form fields here
* Below is an attempt to select all form fields for current set
*/
$(this + ' input, '+ this + ' select').each(function() {
var strName = $(this).attr('name') + intCount;
$(this).attr('name', strName);
});
intCount++;
});
Frustratingly, I had actually tried the correct solution in advance but had forgotten to use the comma at the time!
var intCount = 1;
$('.batch-paste-area .batch-piece').each(function(){
/*
* Would like to be able to loop through form fields here
* Below is an attempt to select all form fields for current set
*/
$(this).find("input, select").each(function() {
var strName = $(this).attr('name') + intCount;
$(this).attr('name', strName);
});
intCount++;
});