I just updated the code and below is the full version of the code
If data returns 0 I want to get the text value of the selected option and append it to a div. Now data returns 0 and the div with class iconSuccess shows up but the value of $a cannot be gotten. I try to alert($a) or even alert(1) in the if condition statement but nothing happens. I need to get the value of $a in the if condition statement. How can I pass the value to the function that return data?
below is the full version of the code
$('#categoryList').change(function () {
$a = $("#categoryList option:selected").text();
$( ".iconSuccess" ).hide();
$("#categoryList5,#categoryList4,#categoryList3,#categoryList2" ).hide();
$.post( "/trobay/categories/default/lists?parent_id="+$(this).val(), function(data) {
if(data !=='0'){
$( "select#categoryList1" ).show();
$( "select#categoryList1" ).html( data );
}else{
$("#categoryList5,#categoryList4,#categoryList3,#categoryList2,#categoryList1" ).hide();
$( ".iconSuccess" ).show();
$( ".mydiv" ).append($a);
alert(1);
alert($a);
}
//alert($a);
});
});
The below image is a screenshot of the message i got back from the server and the green icon only shows up when data return "0"
I hope this put more light on the question and any help will be apppreciated
Related
I have 2 Bootstrap Select dropdowns, one is a list of countries, and another is a list of states. The country list is static and is populated on page load. The list of states is only populated via the Bootstrap Change event and loads states based upon the country.
I need to be able to populate both these values at the same time. So the problem is I'm trying to set the state value when its undefined ,or hasn't loaded its options yet. I've tried doing a callback function within the ajax call, but that doesn't appear to work. I think another problem is that there is the initial change binding that is goofing it up.
I've written a fiddle for my foundation on where I'm experiencing the problem.
https://jsfiddle.net/jeffbeagley/DTcHh/37843/
The user is presented with a blank form that allows them to select the values, but the user can also load a saved form from the database ( hence the function labled as "select_data" ).
$(document).ready(function() {
$( "#country" ).on( 'changed.bs.select', function(e) {
change_country($( this ).val());
});
$( "#load" ).click(function() {
select_data();
});
});
This function is called anytime the user selects a country, the actual ajax call goes out to the database and returns all the appropriate states. I tried passing the state into this function and selecting it that way, but setting the country first would still fire the country change event and override it.
function change_country(country_id) {
$.ajax({
type: 'GET',
cache: false,
url: '/echo/jsonp/',
success: function(response) {
$( "#state" ).empty();
if(country_id == 1) {
$( "#state" ).append( "<option value=1>Oklahoma</option" );
$( "#state" ).append( "<option value=2>Missouri</option" );
} else {
$( "#state" ).append( "<option value=1>Ontario</option" );
$( "#state" ).append( "<option value=2>Quebec</option" );
}
$( "#state" ).selectpicker( "refresh" );
}
});
}
This is the function that grabs the saved form from the database and populates the values. With the way that its set, the expected outcome would be to select the country Canada, and the state Quebec
function select_data() {
var country_id = 2
var state_id = 2
$( "#country" ).selectpicker('val', country_id);
$( "#state" ).selectpicker('val', state_id);
}
Thanks for any help!
Might've figured it out.. sometimes you just need to strip out your code and simplify it to find an answer..
I replaced the code to populate the states outside of the event being called from the country being changed. Then I declared a callback function within this new function on success. I then moved the logic to select the country into the populate_state function ( this may not be appropriate )
You can see the updated fiddle here,
https://jsfiddle.net/jeffbeagley/DTcHh/37849/
So now the function select_data is as follows
function select_data() {
var country_id = 2
var state_id = 2
populate_states(country_id, function() {
$( "#state" ).selectpicker('val', state_id);
});
}
Edit ----
So with this, I ran into a call stack issue as it was still applying the original change event. So when the function select_data() is called, I unbind any events attached to the country selector, and rebind within the callback.
function select_data() {
var country_id = 2
var state_id = 2
$( "#country" ).off( 'changed.bs.select');
$( "#country" ).selectpicker('val', country_id);
populate_states(country_id, function() {
$( "#state" ).selectpicker('val', state_id);
$( "#country" ).on( 'changed.bs.select', function(e) {
change_country(e,$( this ).val());
});
});
}
Basically I want to slide down a element, then when the button is hit again I want it to slide up, empty the div, then slide down with the new results. I'v been trying to figure out how to do this for so long and I cant seem to get it working with jquery.
$(".search").on("click",function(){
$('.results').slideUp(500).empty().append("<p>Results</p>").hide().slideDown(500)
});
I understand this is kind of specific to my project kind of but I do feel others might find this useful
I'm not sure what exactly your problem is, but I think the slideUp animation is not shown in your example.
The slideUp method takes a second argument, which is the function callback. It is called when the slideUp action is finished. If you do the rest of your actions in this callback function, is guaranteed to be performed after the slideUp.
jQuery('#testbutton').on('click',function() {
$('#testlist').slideUp(500, function() {
$('#testlist').empty().append("<li>this is a test</li>").slideDown(500);
});
});
You can find a fully working example here:
https://jsfiddle.net/dxyybwyh/5/
I want to slide down a element, then when the button is hit again I
want it to slide up, empty the div, then slide down with the new
results.
It seems simple enough to me
let me know if you need anything more
$('#myButton').click(function () {
if ( $( ".myDiv" ).is( ":hidden" ) ) {
//show the div
$( ".myDiv" ).slideDown( "slow" );
//add content
$( ".myDiv" ).html("New Content")
} else {
//hide the div
$( ".myDiv" ).slideUp( "slow" );
//clear content
$( ".myDiv" ).html("");
}
});
http://codepen.io/Rohithzr/pen/jqmGXg
Updated the pen with append ability and more readability
$('#myButton').click(function () {
if ( $( ".myDiv" ).is( ":hidden" ) ) {
show();
} else {
hide();
clearContent();
appendContent("New Content");
}
});
function clearContent(){
//clear content
$( ".myDiv" ).html("");
}
function appendContent(content){
$( ".myDiv" ).html($( ".myDiv" ).html()+content);
}
function hide(){
//hide the div
$( ".myDiv" ).slideUp( "slow" );
}
function show(){
//show the div
$( ".myDiv" ).slideDown( "slow" );
}
I have a div in a webpage where I need to load only the last n-th lines of a textfile.
For example:
<div id="contentdiv" style="white-space: pre-wrap;">here comes the output of the logfile</div>
<script>
$(function() {
$( "#contentdiv" ).load( "logs/logfile.log" );
});
</script>
But I only need the last 5 lines of the logfile.
Assuming that line separator is \n
Using the load method will limit you to first load everything and then remove
try
$( "#contentdiv" ).load( "logs/logfile.log", function(response){
$( "#contentdiv" ).html(response.split("\n").slice(-5).join("\n"));
} );
But this will show all the lines first and then only last 5.
So, you need to hide this div first
$( "#contentdiv" ).hide();
$( "#contentdiv" ).load( "logs/logfile.log", function(response){
$( "#contentdiv" ).html(response.split("\n").slice(-5).join("\n"));
$( "#contentdiv" ).show();
} );
Another option could be
$.get( "logs/logfile.log", function(data) {
$( "#contentdiv" ).html(data.split("\n").slice(-5).join("\n"));
})
This will load the div only once with last 5 lines.
So I've taken over a project that is almost ready to launch, a site where you can lease cars. I've uploaded the main part of the service here: http://erikblomqvist.se/junk/car/
Everything works smoothly except for the color changing function (the colored boxes under the headline Färgalternativ). It's supposed to update the price on the colors from brown to light gray (#4 - #8) – those are a bit more expensive, since they are in metallic.
In Chrome, this works as planned, but in Firefox, if I first select a metallic color, then a non-metallic, and THEN a the same metallic again, the price won't change back. It changes correctly the first time, but not the second time I click that metallic color.
In Safari, the price doesn't change at all (I'm guessing that if the Firefox problem gets solved, Safari gets solved as well).
The function is based on a data-name on the color boxes, that gets checked with this function:
$( '#car-colors .color' ).each(function() {
$( this ).on( 'click', function() {
selected_color = undefined;
var color_name = $( this ).data('name');
$( '#car-colors .color' ).not( this ).removeClass('selected');
$( this ).addClass('selected');
$( 'option', color_select ).each( function() {
if( $( this ).val() == color_name ) {
color_select.find( 'option' ).removeAttr('selected');
$( this ).attr('selected', 'selected');
}
});
$( '.selected-color-name' ).fadeIn();
$( '.selected-color-name span' ).html( color_name );
var selected_color = color_select.children(':selected');
checkSelectedColor(selected_color);
});
});
The variable color_select is defined as $( '#order-color-select' ).
The function checkSelectedColor is defined here:
function checkSelectedColor(selected_color) {
if( selected_color.data('is-metallic') == 'yes' ) {
color_checkbox.prop('checked', true);
} else {
color_checkbox.prop('checked', false);
}
color_input.val( selected_color.val() );
calculatePrice();
}
I've added stuff like selected_color = undefined to make sure that the variable is reseted, but after a color that has an <option data-is-metallic="yes"> (inside of #order-color-select) is selected a second time, it handles the value as "no" instead of "yes".
I can't get my head around on why this is, especially only in Firefox/Safari.
I've included the beautified version of the car functions here: http://pastebin.com/i5bup5rx
Appreciate any kind of help that could lead me in the right direction of getting this solved!
Thanks
I think the error is in setting the selected option in the select element using .attr().
Instead you can just set the value of the select element like
$('#car-colors .color').on('click', function() {
var color_name = $(this).data('name');
$('#car-colors .color').not(this).removeClass('selected');
$(this).addClass('selected');
$('#order-color-select').val(color_name);
$('.selected-color-name').fadeIn();
$('.selected-color-name span').html(color_name);
var selected_color = color_select.children(':selected');
checkSelectedColor(selected_color);
});
I trying to get a trigger working but this doesn't seem to work. Im 100% sure this is the right code to do the job.
$(document).ready(function() {
$( ".relrightbutton" ).click(function() {
$( ".attachment-large" ).trigger( "click" );
});
});
however is there another way to trigger an element click when a different element is clicked?
I don't see flaws in that code. It might be something else then (classnames are correct?)
You could try getting the click on another thread:
$(document).ready(function() {
$( ".relrightbutton" ).click(function() {
setTimeout(function(){
$( ".attachment-large" ).trigger( "click" );
}, 10);
});
});
Or try another click method trigger:
$(document).ready(function() {
$( ".relrightbutton" ).click(function() {
$( ".attachment-large" ).click();
});
});
Is there no error message provided in your console?