i newbie in programming and wanna ask how to change button 2 way.
<button type="button" id="button">Save</button>
<script>
$(document).ready(function() {
$('#button').click(function(){
$('#button').text("Edit");
});
});
initial when i click i turn to "Edit" then how to click back to "Save" when clicking.
thank you
Write a small toggle function:
$('#button').on('click', function() {
$(this).text(function(_, value) {
return value == 'Save' ? 'Edit' : 'Save';
});
});
$(document).ready(function() {
var editing = false;
$('#button').click(function(){
editing = !editing;
if (editing) {
$('#button').text("Edit");
} else {
$('#button').text("Save");
}
});
});
check this:
$(document).ready(function() {
$('#button').click(function(){
if( $('#button').text()=='Save')
{
$('#button').text("Edit");
}
else
{
$('#button').text("Save");
}
});
});
http://jsfiddle.net/2m5q7/
A simple ternary operator will do it:
$('#button').click(function(){
$(this).text($(this).text() == 'Save' ? 'Edit' : 'Save');
});
Or you can pass a function to .text():
Demo
$('#button').click(function(){
$(this).text(function(_, val){
return val == 'Save' ? 'Edit' : 'Save';
});
});
Related
I am using checkbox on every row to do multi delete record in datatable. I want to show delete button only if any checkbox is checked. On first page, onchange is working. But on second page so on, onchange not working.
Below is my code :
$(".isdt-selected").on("change", function() {
$(".isdt-selected").each(function(index, elem) {
if($(elem).is(':checked')){
$('#btn-delete-bulk').show();
return false;
}else{
$('#btn-delete-bulk').hide();
}
});
});
I solved it with below code :
"drawCallback": function(settings) {
$(".isdt-selected").on("change", function() {
$(".isdt-selected").each(function(index, elem) {
if($(elem).is(':checked')){
$('#btn-delete-bulk').show();
return false;
}else{
$('#btn-delete-bulk').hide();
}
});
});
I saw the reference from here https://datatables.net/reference/option/drawCallback
Try this:
$(document).ready(function(){
$(".isdt-selected").on("change", function() {
$(".isdt-selected").each(function(index, elem) {
if($(elem).is(':checked')){
$('#btn-delete-bulk').show();
return false;
}else{
$('#btn-delete-bulk').hide();
}
});
});
});
Why my function hide/show element base on radio button not work ?
http://jsfiddle.net/qmzz9x7h/
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
if($(#radio_id).attr("value")=="first"){
$("#second_checkbox").hide();
$("#first_checkbox_display").show();
}
if($(#radio_id).attr("value")=="second"){
$("#first_checkbox_display").hide();
$("#second_checkbox").show();
}
});
});
</script>
your fiddle is wrong, take look on short and improved version:
$(document).ready(function(){
$('input[type="radio"]').click(function(e){
var _val = $(this).val();
var _id = $(this).attr('id'); // Grap the ID
$("#second_checkbox, #first_checkbox_display").hide();
console.log(_val);
if( _val == "first" ){
$("#"+_val+"_checkbox_display").show();
}
else if( _val =="second"){
$("#"+_val+"_checkbox").show();
}
});
});
Demo
<script>
$(document).ready(function(){
$(".radio_id").click(function(){
if($(this).attr("value")=="first"){
$("#second_checkbox").hide();
$("#first_checkbox_display").show();
}
if($(this).attr("value")=="second"){
$("#first_checkbox_display").hide();
$("#second_checkbox").show();
}
});
});
</script>
Give your radio button a class "radio_id"
I am trying to use the same button to trigger an ajax call to add a database entry if it is clicked and then trigger a different ajax call to remove the entry it is clicked again.
I have tried using toggleClass and although the button class does change and it's appearance changes accordingly the function still thinks it has the old class name.
$(document).ready(function() {
$(".selected").on("click", function() {
$(this).text(function (i, oldText) {
return $.trim(oldText) == 'Use Image' ? 'Selected' : 'Use Image';
});
$(this).toggleClass('selected selected_btn');
});
$(".selected").on("click", function() {
alert('selected');
});
$(".selected_btn").on("click", function() {
alert('de selected');
});
});
With the present code the alert is always 'selected'.
$(document).ready(function() {
$(".selected_btn").on("click", function() {
$(this).text(function (i, oldText) {
return $.trim(oldText) == 'Use Image' ? 'Selected' : 'Use Image';
});
$(this).toggleClass('selected');
if($(this).hasClass("selected"))
alert("Selected")
else
alert("de-Selected")
});
});
here is a fiddle:
http://fiddle.jshell.net/prollygeek/3LLN2/
Here is a simple and readable example on how to do this:
$(document).ready(function() {
$('.select-img').on('click', function(){
var $el = $(this);
var isSelected = $el.attr('data-selected');
if( isSelected != 'true' ){
firstFn();
$el.html('Use Image').attr('data-selected', true)
}else{
secondFn();
$el.html('Select').attr('data-selected', false)
}
})
var firstFn = function(){
alert('first thing to do');
}
var secondFn = function(){
alert('second thing to do');
}
})
Demo
Use *Class functions:
hasClass
removeClass
addClass
Working code:
$("a").on("click", function() {
if($(this).hasClass("bob")) {
// do delete
alert("delete");
$(this).removeClass("bob");
} else {
// do insert
alert("insert");
$(this).addClass("bob");
}
});
Demo
$(".selected").on("click", function() {
alert('selected');
});
Overrides the event you put on the beginning of the document.ready, I think.
(might not be true, but I think it is)
I have a div that expands once you click on a link; my issue, is I want to change the text while the div expands so that the link says "hide form". Can somebody please help me? I'm still new to jQuery..
The link to the page is here: http://biggz.co
Here's the jQuery I'm currently using:
$(document).ready(function()
{
jQuery('a.contactForm').click(function() {
jQuery('#contactForm').slideToggle('slow', function() {
// Animation complete.
});
});
});
jQuery('#contactForm').slideToggle('slow', function () {
var txt = ($(this).css('display') == 'none') ? 'contact us directly':'Hide Form';
$('.contactForm').text(txt);
});
$(document).ready(function()
{
jQuery('a.contactForm').click(function() {
jQuery('#contactForm').slideToggle('slow');
$(this).text($(this).text() == 'Show' ? 'Hide' : 'Show');
});
});
Make use of text(). Like
jQuery('a.contactForm').text('Hide Form');
So your code can be
jQuery('a.contactForm').click(function() {
var text= $(this).text();
if (text == 'Show Form')
$(this).text('Hide From');
else
$(this).text('Show Form');
jQuery('#contactForm').slideToggle('slow', function() {
// Animation complete.
});
});
I need to use jquery to toggle two elements to show/hide. I want the button to say "hide text" and change to "show text" when it is clicked and of course I want the text to toggle from show to hide as the button is clicked. I have this to change it one time but I do not know how to change it back or make it toggle.
$(function() {
$('button#1').click(function() {
$('#one').fadeOut();
});
$('button#1').click(function() {
$('button#1').html('Show Text');
});
});
http://jsfiddle.net/fwdzm/1/
Use the toggle callbacks !
$(function() {
var $btn1 = $('button#1').click(function() {
$('#one').toggle('slow', function () {
if ($(this).is(':visible')) {
$btn1.html('Hide Text');
} else {
$btn1.html('Show Text');
}
});
});
});
$('button#1').click(function() {
var $btn = $(this);
$('#one').toggle('slow', function() {
if ($(this).is(':visible')) {
$btn.text('Hide');
} else {
$btn.text('Show Text');
}
});
});
Try something like this
$(function() {
$('button#1').click(function(){
if ($('#one').is(':visible')) {
$('#one').fadeOut();
$(this).html('Show Text');
} else {
$('#one').fadeIn();
$(this).html('Hide Text');
}
});
});
Save the current state in a variable and then based on that variable you either show or hide your elements and change the text on the button.
HTML
<button id="toggleBtn" type="button">Hide Text</button>
<span id="element">Your text is here</span>
JAVASCRIPT
var status = "shown";
function toggleElement() {
if (status == "shown") {
$('#element').hide();
$("#toggleBtn").html('Show Text');
status = "hidden";
} else {
$('#element').show();
$("#toggleBtn").html('Hide Text');
status = "shown";
}
}