I have created a jquery method to load data when selects from the dropdown but the problem is that when i click and select and data changes then again when I select it is not working anymore
Please take a look at below snippet what i have done so far
$(document).ready(function() {
$('select[name="ht_opt"]').change(function() {
var tab = $('select[name="ht_opt"]').val();
var symbol = $('#symbol').val();
if ($(this).val()){
var dropdown = "<select name='ht_opt'><option>Balance Sheet</option><option>Profit and Loss Account</option><option>Cash Flow Statement</option></select>";
$('#ht_title').html(tab + " " + dropdown);
$('#ht_data').html(symbol);
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="inf_h2" id="ht_title">Balance Sheet
<select name="ht_opt">
<option>Balance Sheet</option>
<option>Profit and Loss Account</option>
<option>Cash Flow Statement</option>
</select>
</h2>
<input type="hidden" id="symbol" value="test Symbol" />
<div id="ht_data">
</div>
Demo fiddle
you could use on() function of jquery .because you are append the select tag on each time of the change function .
Just follow the #devnull69 comment below use the first option like this <option style='color:#ccc'>--select option--</option>
$(document).ready(function() {
$(document).on('change','select[name="ht_opt"]',function() {
var tab = $(this).val();
var symbol = $('#symbol').val();
if ($(this).val()) {
var dropdown = "<select name='ht_opt'><option style='color:#ccc'>--select option--</option><option>Balance Sheet</option><option>Profit and Loss Account</option><option>Cash Flow Statement</option></select>";
$('#ht_title').html(tab + " " + dropdown);
$('#ht_data').html(symbol);
}
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="inf_h2" id="ht_title">Balance Sheet
<select name="ht_opt">
<option style="color:#ccc">--select option--</option>
<option>Balance Sheet</option>
<option>Profit and Loss Account</option>
<option>Cash Flow Statement</option>
</select>
</h2>
<input type="hidden" id="symbol" value="test Symbol" />
<div id="ht_data">
</div>
Updated answer : its show the selected value without reproduce the select tag
$(document).ready(function() {
$(document).on('change', 'select[name="ht_opt"]', function() {
var tab = $(this).val();
var symbol = $('#symbol').val();
$('select[name="ht_opt"] option').removeClass('select')
$('select[name="ht_opt"] option:selected').addClass('select')
if (tab) {
$('#ht_title p').html(tab);
$('#ht_data').html(symbol);
}
})
})
.select{
background:#aaa;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="inf_h2" id="ht_title">
<p>Balance Sheet</p>
<select name="ht_opt">
<option class="select">Balance Sheet</option>
<option>Profit and Loss Account</option>
<option>Cash Flow Statement</option>
</select>
</h2>
<input type="hidden" id="symbol" value="test Symbol" />
<div id="ht_data">
</div>
Try to use delegate.But As of jQuery 3.0, .delegate() has been deprecated. It was superseded by the .on() method
Snippet
$(document).ready(function() {
$('body').delegate('select[name="ht_opt"]','change', function(){
var tab = $('select[name="ht_opt"]').val();
var symbol = $('#symbol').val();
if ($(this).val()){
var dropdown = "<select name='ht_opt'><option>Balance Sheet</option><option>Profit and Loss Account</option><option>Cash Flow Statement</option></select>";
console.log(dropdown);
$('#ht_title').html(tab + " " + dropdown);
$('#ht_data').html(symbol);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2 class="inf_h2" id="ht_title">Balance Sheet
<select name="ht_opt">
<option>Balance Sheet</option>
<option>Profit and Loss Account</option>
<option>Cash Flow Statement</option>
</select>
</h2>
<input type="hidden" id="symbol" value="test Symbol" />
<div id="ht_data">
</div>
Here is the jsfiddle:https://jsfiddle.net/nc425yh1/9/
Related
I would like to change a part of the main link using a selector. For example:
There is a main link that we are redirected to by default, but, if we select an option on the selector, it will add a complement on the link, redirecting us to another page.
How can I get it to work the way I would like to?
jQuery(document).ready(function() {
jQuery("#buttonId").click(function() {
var instagram_value = $('#instagram').val();
var instagram_name = $('#instagram :selected').text();
window.location = "https://instagram.com/" + "+instagram_name + " / " +instagram_name +" / " + country_value;
});
});
<form method="get" action="">
<select id="instagram">
<option value="">Select Location</option>
<option value="1">The Rock</option>
<option value="2">Will Smith</option>
</select>
<button type="submiti" id="buttonId">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
If you don't want the form to be submitted and instead just want to rediredt to another page, just set the button type to button:
<button type="button" id="buttonId">Submit</button>
Working example:
(simplified for demonstration)
jQuery(document).ready(function() {
jQuery("#buttonId").click(function() {
var instagram_value = $('#instagram').val();
var instagram_name = $('#instagram :selected').text();
window.location = "https://instagram.com/" + instagram_name;
});
});
<form method="get" action="">
<select id="instagram">
<option value="">Select Location</option>
<option value="1">The Rock</option>
<option value="2">Will Smith</option>
</select>
<button type="button" id="buttonId">Submit</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
Here is my script, what my goal is if other is selected in select, the other text input beside it will be enabled, this is what i've got so far, any approach will be really appreciated, I have 4 questions like this and I want it to be modular, best approach for doing my function to be reuseable.. How do I properly do this without any problem posting my data as 2 name inputs will generate 2 post variables in php.. T_T
<script type="text/javascript" charset="utf-8">
function validate()
{
var ddl = document.getElementById("cause_pain");
var selectedValue = ddl.options[ddl.selectedIndex].value;
if (selectedValue == "OTHER")
{
document.getElementsByClassName("causepain")[0].removeAttribute("name");
document.getElementsByClassName("causepain1")[0].removeAttribute("disabled");
}
}
</script>
<form action="test.php" method="GET">
<select class="select causepain" id="cause_pain" name="cause_pain" onchange="validate()">
<option value="" selected="selected">Select Cause of Pain</option>
<option value="ARTHRITIS">ARTHRITIS</option>
<option value="RHEUMATISM">RHEUMATISM</option>
<option value="OLD AGE">OLD AGE</option>
<option value="ACTIVE LIFESTYLE WHEN YOUNGER">ACTIVE LIFESTYLE WHEN YOUNGER</option>
<option value="OTHER">OTHER</option>
</select>
<input class="causepain1" type="text" id="cause_pain" name="cause_pain" size="40" onkeyup="clean('this.id')" disabled>
<input type="submit" id="submit"/>
</form>
This method is reusable and pretty straight forward. Using data attributes, you could specify the element that needs to be shown on the specific option element. Also before showing any input element hide the elements that were attributed to the previous selection.
Example:
<form action="test.php" method="GET">
<select class="select causepain" id="cause_pain" name="cause_pain">
<option value="" selected="selected">Select Cause of Pain</option>
<option value="ARTHRITIS">ARTHRITIS</option>
<option value="RHEUMATISM">RHEUMATISM</option>
<option value="OLD AGE">OLD AGE</option>
<option value="ACTIVE LIFESTYLE WHEN YOUNGER">ACTIVE LIFESTYLE WHEN YOUNGER</option>
<option value="OTHER" data-show="cause_pain_other">OTHER</option>
</select>
<input class="causepain1" type="text" id="cause_pain_other" name="cause_pain" size="40"
onkeyup="clean('this.id')" disabled style="display: none;">
<input type="submit" id="submit"/>
</form>
<script type="text/javascript" charset="utf-8">
var selectedOpt;
function selectionChanged(e) {
if (selectedOpt && selectedOpt.dataset.show) {
var showEl = document.getElementById(selectedOpt.dataset.show);
showEl.disabled = true;
showEl.style.display = 'none';
}
selectedOpt = this.querySelector('[value="'+e.target.value+'"]');
if (selectedOpt.dataset.show) {
var showEl = document.getElementById(selectedOpt.dataset.show);
showEl.disabled = false;
showEl.style.display = 'block';
}
}
document.querySelector('select').addEventListener('change', selectionChanged);
</script>
Your selectedOpt should be an object if you're using multiple selects on the same page and then just add the element to the object with the id as an index:
var selectedOpt = {};
...
selectedOpt[this.id] = this.querySelector('[value="'+e.target.value+'"]');
I'm creating a form that allows a user to select an amount of services, and then a total is displayed. I have it working to where I can keep the running total of one drop down menu service, but then I can't figure out how to add additional services from other drop down menus. For example, I need it so that if someone selects '3' from one drop down menu, it will times that value by 10 (I have that part done), but then also be able to select a '4' from the next dropdown menu and times that by a value, and then display a total.
Fiddle: https://jsfiddle.net/7jrch7w6/
I'm very new to JavaScript and JQuery, so feel free to give suggestions if you see a better way or if my code is garbage. Thanks!
Here's my code:
<form>
<div id="carpet-services">
<div class="form-group">
<!--how many bedrooms-->
<label class="control-label" for="carpet_cleaning">Bedrooms</label>
<select class="form-control" id="carpet_cleaning">
<option value="0-bedroom">0</option>
<option value="1-bedroom">1</option>
<option value="2-bedroom">2</option>
</select>
<label class="hide" for="0-bedroom">0</label>
<label class="hide" for="1-bedroom">1</label>
<label class="hide" for="2-bedroom">2</label>
<!--how many dining rooms-->
<label class="control-label" for="dining_rooms">Dining Rooms</label>
<select class="form-control" id="dining_rooms">
<option value="0-diningRoom">0</option>
<option value="1-diningRoom">1</option>
<option value="2-diningRoom">2</option>
</select>
<label class="hide" for="0-diningRoom">0</label>
<label class="hide" for="1-diningRoom">1</label>
<label class="hide" for="2-diningRoom">2</label>
</div>
</div>
<h4>Total Price</h4>
<p id="output1"></p>
JS
$("#carpet_cleaning").change(function () {
var bedrooms = $("#carpet_cleaning").val();
var diningRooms = $("#dining_rooms").val();
var bedroomPrice = '';
var diningRoomPrice = '';
if (carpet_cleaning){
var bedroomsLabel = $("label[for="+bedrooms+"]").text();
bedroomPrice = 'Your price quote is: ' + bedroomsLabel * 10;
}
$("#output1").text(bedroomPrice).fadeIn();
});
One line of CSS:
label.hide {display: none;}
First of all, there's no need to use labels for every drop down option, that's what value attribute is for:
<form>
<div id="carpet-services">
<div class="form-group">
<!--how many bedrooms-->
<label class="control-label" for="carpet_cleaning">Bedrooms</label>
<select class="form-control" id="carpet_cleaning">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<!--how many dining rooms-->
<label class="control-label" for="dining_rooms">Dining Rooms</label>
<select class="form-control" id="dining_rooms">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<h4>Total Price</h4>
<p id="output1"></p>
</form>
Calculation can now be simplified and properly hooked up to change event of both drop downs:
$("#carpet_cleaning,#dining_rooms").change(function () {
var bedrooms = $("#carpet_cleaning").val();
var diningRooms = $("#dining_rooms").val();
var total = bedrooms * 10 + diningRooms * 20;
var totalPrice = 'Your price quote is: ' + total;
$("#output1").text(totalPrice).fadeIn();
});
I don't know if I got your question, but you can use $("#carpet_cleaning option:selected").val() to get the current selected value in a select component.
Fiddle: https://jsfiddle.net/7jrch7w6/4/
$("#carpet_cleaning").change(function () {
UpdateResult();
});
$("#dining_rooms").change(function () {
UpdateResult();
});
function UpdateResult()
{
var result = 'Your price quote is: ' + (($("#carpet_cleaning option:selected").val() * 10) + ($("#dining_rooms option:selected").val() * 20))
$("#output1").text(result).fadeIn();
}
I have two divs, .div_data and .div_update where
<select name=change_placement>
<option value=0>Top</option>
<option value=1>Bottom</option>
</select>
<div class='div_data'>Data Data</div>
<div class='div_update'>
<form name=stuff>
<input type=text>
</form>
</div>
How do I get .div_update to precede .div_data when clicking on select[name=change_placement]?
I put an id to your select and also added an option 'Select' to your dropdown select. I am assuming that you want the input text box to go above the other div when you choose 'Top'.
$('#change_placement').on('change', function() {
var option = $(this).val();
if(option == 0) {
$('.div_update').insertBefore($('.div_data'));
} else {
$('.div_update').insertAfter($('.div_data'));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='change_placement' id='change_placement'>
<option value=''>Select</option>
<option value='0'>Top</option>
<option value='1'>Bottom</option>
</select>
<div class='div_data'>
Data
Data
</div>
<div class='div_update'>
<form name=stuff>
<input type=text>
</form>
</div>
Make use of insertBefore and insertAfter functions
$(function(){
$('select').on('change', function(){
var value = $(this).val();
if(value == "1") {
$('.div_update').insertAfter('.div_data');
} else {
$('.div_update').insertBefore('.div_data');
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name=change_placement>
<option value="0">Top</option>
<option value="1">Bottom</option>
</select>
<div class='div_data'>
Data
Data
</div>
<div class='div_update'>
<form name=stuff>
<input type=text>
</form>
</div>
emphasized text
Quick play fixed issue:
if(update_ticket_placement == 0) {
var myparent = $('div.div_update');
var parentparent = $('div.div_data');
myparent.detach().prependTo(parentparent );
}
You can use .insertAfter() to inserting targer element after specific element.
$("select").change(function(){
var value = $(this).val();
$(".div_update").insertAfter(value == 1 ? ".div_data" : this);
});
$("select").change(function(){
$(".div_update").insertAfter($(this).val() == 1 ? ".div_data" : this);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name=change_placement>
<option value=0>Top</option>
<option value=1>Bottom</option>
</select>
<div class='div_update'>
<form name=stuff>
<input type=text>
</form>
</div>
<div class='div_data'>Data Data</div>
I know this is simple, and I need to search in Google. I tried my best and I could not find a better solution. I have a form field, which takes some input and a select field, which has some values. It also has "Other" value.
What I want is:
If the user selects the 'Other' option, a text field to specify that 'Other' should be displayed. When a user selects another option (than 'Other') I want to hide it again. How can I perform that using JQuery?
This is my JSP code
<label for="db">Choose type</label>
<select name="dbType" id=dbType">
<option>Choose Database Type</option>
<option value="oracle">Oracle</option>
<option value="mssql">MS SQL</option>
<option value="mysql">MySQL</option>
<option value="other">Other</option>
</select>
<div id="otherType" style="display:none;">
<label for="specify">Specify</label>
<input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>
Now I want to show the DIV tag**(id="otherType")** only when the user selects Other.
I want to try JQuery. This is the code I tried
<script type="text/javascript"
src="jquery-ui-1.10.0/tests/jquery-1.9.0.js"></script>
<script src="jquery-ui-1.10.0/ui/jquery-ui.js"></script>
<script>
$('#dbType').change(function(){
selection = $('this').value();
switch(selection)
{
case 'other':
$('#otherType').show();
break;
case 'default':
$('#otherType').hide();
break;
}
});
</script>
But I am not able to get this. What should I do? Thanks
You have a few issues with your code:
you are missing an open quote on the id of the select element, so: <select name="dbType" id=dbType">
should be <select name="dbType" id="dbType">
$('this') should be $(this): there is no need for the quotes inside the paranthesis.
use .val() instead of .value() when you want to retrieve the value of an option
when u initialize "selection" do it with a var in front of it, unless you already have done it at the beggining of the function
try this:
$('#dbType').on('change',function(){
if( $(this).val()==="other"){
$("#otherType").show()
}
else{
$("#otherType").hide()
}
});
http://jsfiddle.net/ks6cv/
UPDATE for use with switch:
$('#dbType').on('change',function(){
var selection = $(this).val();
switch(selection){
case "other":
$("#otherType").show()
break;
default:
$("#otherType").hide()
}
});
UPDATE with links for jQuery and jQuery-UI:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
Demo on JSFiddle
$(document).ready(function () {
toggleFields(); // call this first so we start out with the correct visibility depending on the selected form values
// this will call our toggleFields function every time the selection value of our other field changes
$("#dbType").change(function () {
toggleFields();
});
});
// this toggles the visibility of other server
function toggleFields() {
if ($("#dbType").val() === "other")
$("#otherServer").show();
else
$("#otherServer").hide();
}
HTML:
<p>Choose type</p>
<p>Server:
<select id="dbType" name="dbType">
<option>Choose Database Type</option>
<option value="oracle">Oracle</option>
<option value="mssql">MS SQL</option>
<option value="mysql">MySQL</option>
<option value="other">Other</option>
</select>
</p>
<div id="otherServer">
<p>Server:
<input type="text" name="server_name" />
</p>
<p>Port:
<input type="text" name="port_no" />
</p>
</div>
<p align="center">
<input type="submit" value="Submit!" />
</p>
You have to use val() instead of value() and you have missed starting quote id=dbType" should be id="dbType"
Live Demo
Change
selection = $('this').value();
To
selection = $(this).val();
or
selection = this.value;
I got its answer. Here is my code
<label for="db">Choose type</label>
<select name="dbType" id=dbType">
<option>Choose Database Type</option>
<option value="oracle">Oracle</option>
<option value="mssql">MS SQL</option>
<option value="mysql">MySQL</option>
<option value="other">Other</option>
</select>
<div id="other" class="selectDBType" style="display:none;">
<label for="specify">Specify</label>
<input type="text" name="specify" placeholder="Specify Databse Type"/>
</div>
And my script is
$(function() {
$('#dbType').change(function() {
$('.selectDBType').slideUp("slow");
$('#' + $(this).val()).slideDown("slow");
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function myfun(){
$(document).ready(function(){
$("#select").click(
function(){
var data=$("#select").val();
$("#disp").val(data);
});
});
}
</script>
</head>
<body>
<p>id <input type="text" name="user" id="disp"></p>
<select id="select" onclick="myfun()">
<option name="1"value="1">first</option>
<option name="2"value="2">second</option>
</select>
</body>
</html>
$('#dbType').change(function(){
var selection = $(this).val();
if(selection == 'other')
{
$('#otherType').show();
}
else
{
$('#otherType').hide();
}
});