How to make a single button appear on only 1 jquery function - html

I have a button called add to profile, all I need to know is how do I make it so its only on the last jQuery div, after the last next.
Like question 1, then next, question 2 back and next, question 3, back and add button.
So if you can really help me out here quick I'd really appreciate it.
Here's my code:
<script>
$(document).ready(function () {
var oldOption;
var counter = 1;
var maxThings = 4;
var minThings = 1;
$("#2").hide();
$("#3").hide();
$("#4").hide();
$('#back').hide();
$("#forward").click(function () {
$("#1").hide();
$("#2").hide();
$("#3").hide();
$("#4").hide();
if (!(counter >= maxThings)) {
counter++;
}
$("#" + counter).show();
$('#back').show();
if (counter === maxThings) {
$('#forward').hide();
}
});
$("#back").click(function () {
$("#1").hide();
$("#2").hide();
$("#3").hide();
$("#4").hide();
if (!(counter <= 1)) {
counter--;
}
$("#" + counter).show();
$('#forward').show();
if (counter === minThings) {
$('#back').hide();
}
});
});
</script>

...
if (counter === maxThings) {
$('#forward').hide();
$("#AddToProfile").show();
} else {
$("#AddToProfile").hide();
}
...
And I really don't like youre coding :) You'll have to change code every time you'll add new div.
I would do something like that:
- Every div would have class question
- Every div's START visibility attribute would be defined in html (every div except first would have in style something like visibility:hidden)
<script>
$(document).ready(function () {
var oldOption;
var counter = 1;
var maxThings = 4;
var minThings = 1;
$('#back').hide();
$("#forward").click(function () {
$("div.question").hide();
if (!(counter >= maxThings)) {
counter++;
}
$("#" + counter).show();
$('#back').show();
if (counter === maxThings) {
$('#forward').hide();
}
});
$("#back").click(function () {
$("div.question").hide();
if (!(counter <= 1)) {
counter--;
}
$("#" + counter).show();
$('#forward').show();
if (counter === minThings) {
$('#back').hide();
}
});
});
</script>

Related

How to call jQuery with button

I am editing a website that is automated with java, I am creating a menu to hide few divs that are repated with same id and same class. I managed to do it already with jQuery with this:
$(document).ready(function () {
$('[id]').each(function () {
var ids = $('[id=catalog_container' + this.id + ']');
if (ids.length > 1 && ids[0] == this) {
$(ids[0]).remove();
}
});
});
I need to do it in another way, but I don't know how to do it. I want to do it with button.
<li><a><button onclick="myFunction()">1 Catalog</button></a></li>
Something like that, when I press that button, to remove first duplicated id, when I press another button to remove second duplicated ID, and so on.
Help me please.
I'd like to know how to do it like this too: not to load script automatically as it does, I want to know how to call it.
I managed to find a solution, I will post it for anyone to see it, and give feedback if you want!
<!-- Hide second id -->
$(document).ready(function () {
$("#Catalog").click(function () {
$('[id]').each(function () {
var ids = $('[id=' + this.id + ']');
if (ids.length > 1 && ids[0] == this) {
$(ids[1]).hide();
$(ids[0]).show();
}
});
});
});
$(document).ready(function () {
$("#Catalog2").click(function () {
$('[id]').each(function () {
var ids = $('[id=' + this.id + ']');
if (ids.length > 1 && ids[0] == this) {
$(ids[0]).hide();
$(ids[1]).show();
}
});
});
});
<!-- Hide First ID -->
$(document).ready(function () {
$("#Catalog").click(function () {
$('[class]').each(function () {
var ids = $('[class=category-title' + this.id + ']');
if (ids.length > 1 && ids[0] == this) {
$(ids[1]).hide();
$(ids[0]).show();
}
});
});
});
$(document).ready(function () {
$("#Catalog2").click(function () {
$('[class]').each(function () {
var ids = $('[class=category-title' + this.id + ']');
if (ids.length > 1 && ids[0] == this) {
$(ids[0]).hide();
$(ids[1]).show();
}
});
});
});
<ul>
<li><a><button style="color:black;" id="Catalog">1 Catalog</button></a></li>
<li><a><button style="color:black;" id="Catalog2">2 Catalog</button></a></li>
</ul>

Slide Down Div on toggle button

I have the following code which works to create the div on click, but I would like the div to slide down slowly and not appear immediately:
function hideshow(which) {
if (!document.getElementById) return
if (which.style.display == "none") which.style.display = "block"
else which.style.display = "none"
I've found some code that appears to do what I'm after, but I'm having difficulty integrating it into my existing code (above):
$(document).ready(function() {
$("#button").toggle(function() {
$(this).text('Hide');
}, function() {
$(this).text('show');
}).click(function(){
$("#hidden_content").slideToggle("slow");
});
I've stuck it on js fiddle if that helps (with supporting css):
http://jsfiddle.net/dRpWv/479/ and
http://jsfiddle.net/dRpWv/447/
Use jquery property "slideDown"
My Codepen
function hideshow() {
var i = 1;
if (document.getElementById('effet').style.display == "none") {
$('#effet').slideDown("normal");
document.getElementById('effet').style.display = "block";
i = 2;
} else {
if (i == 1) {
$('#effet').slideUp("normal", function () {
document.getElementById('effet').style.display = "none";
});
}
}
}

jquery cookie code error

I tried to create a cookie for the link but it showed me errors like this....how to fix it...
Uncaught ReferenceError: createCookie is not defined
i am providing my code in fiidle
http://jsfiddle.net/SSMX4/82/ cookie not working
my jquery code is below
// locale selector actions
$('#region-picker').click(function(){
if ($("#locale-select").is(":visible")) return closeSelector('slide');
var foot_height = $('#footer').innerHeight();
var foot_height_css = foot_height-1;
var select_position = '-=' + (Number(700)+18);
console.log("hallo"+select_position);
var $selector = $('#locale-select');
$('#locale_pop').fadeOut();
$selector.css({top:foot_height_css});
$selector.fadeIn(function(){
$(this).addClass('open');
$(this).animate({top:select_position}, 1000);
});
});
$('#select-tab').click(function(e){
e.stopPropagation()
closeSelector('slide');
});
// don't hide when clicked within the box
$('#locale-select').click(function(e){
e.stopPropagation();
});
$(document).click(function(){
if ($('#locale-select').hasClass('open')) {
closeSelector('disappear');
}
});
$('.locale-link').click(function(){
//var $clicked = $(this); //"$(this)" and "this" is the clicked span
$(".locale-select-lable").html($(this).html());
//search for "marginXXXXX"
var flags = $(this).attr("class").match(/(margin\w+)\s/g);
//create new class; add matching value if found
var flagClass = "tk-museo-sans locale-select-lable" + (flags.length ? " " + flags[0] : "");
//set new class definition
$(".locale-select-lable").attr("class", flagClass);
closeSelector('disappear');
//if ($("#locale-select").is(":visible")) return closeSelector('slide');
/*
// var desired_locale = $(this).attr('rel');
// createCookie('desired-locale',desired_locale,360);
// createCookie('buy_flow_locale',desired_locale,360);
//closeSelector('disappear');
*/
}); /* CORRECTED */
$('#locale_pop a.close').click(function(){
var show_blip_count = readCookie('show_blip_count');
if (!show_blip_count) {
createCookie('show_blip_count',3,360);
}
else if (show_blip_count < 3 ) {
eraseCookie('show_blip_count');
createCookie('show_blip_count',3,360);
}
$('#locale_pop').slideUp();
return false;
});
function closeSelector(hide_type){
var foot_height = $('#footer').innerHeight();
var select_position = '+=' + (Number(400)+20);
if (hide_type == 'slide') {
$('#locale-select').animate({top:select_position}, 1000, function(){
$(this).removeClass('open');
$(this).fadeOut()
});
}
else if (hide_type == 'disappear'){
$('#locale-select').fadeOut('fast');
$('#locale-select').removeClass('open');
}
}
$('.locale-link').click(function(){
var desired_locale = $(this).attr('rel');
console.log("cookie....." + desired_locale);
createCookie('desired-locale',desired_locale,360);
createCookie('buy_flow_locale',desired_locale,360);
closeSelector('disappear');
})
$('#locale_pop a.close').click(function(){
var show_blip_count = readCookie('show_blip_count');
if (!show_blip_count) {
createCookie('show_blip_count',3,360);
}
else if (show_blip_count < 3 ) {
eraseCookie('show_blip_count');
createCookie('show_blip_count',3,360);
}
$('#locale_pop').slideUp();
return false;
});
​
I did not find your declaration of the function "createCookie". It seems you copied the code and forgot to copy the function.
Btw: "xxx is not defined" always means the variable/function does not exist ..

How to create a HTML5 single line contentEditable tab which listens to Enter and Esc

I have a dashboard at work and I want people to be able to change the text on the tabs in order to create categories which fit them best.
I wanted single line text which would disregard changes on Esc or commit the changes on Enter or Blur.
I found the answer but figured I would post in order to help others.
Here is the HTML markup:
<span contenteditable="false"></span>
Here is the jQuery/javascript:
$(document).ready(function() {
$('[contenteditable]').dblclick(function() {
$(this).attr('contenteditable', 'true');
clearSelection();
$(this).trigger('focus');
});
$('[contenteditable]').live('focus', function() {
before = $(this).text();
if($(this).attr('contenteditable') == "true") { $(this).css('border', '1px solid #ffd646'); }
//}).live('blur paste', function() {
}).live('blur', function() {
$(this).attr('contenteditable', 'false');
$(this).css('border', '1px solid #fafafa');
$(this).text($(this).text().replace(/(\r\n|\n|\r)/gm,""));
if (before != $(this).text()) { $(this).trigger('change'); }
}).live('keyup', function(event) {
// ESC=27, Enter=13
if (event.which == 27) {
$(this).text(before);
$(this).trigger('blur');
} else if (event.which == 13) {
$(this).trigger('blur');
}
});
$('[contenteditable]').live('change', function() {
var $thisText = $(this).text();
//Do something with the new value.
});
});
function clearSelection() {
if ( document.selection ) {
document.selection.empty();
} else if ( window.getSelection ) {
window.getSelection().removeAllRanges();
}
}
Hope this helps someone!!!

How to add an "_blank" attribute inside ajax code?

I'm really a beginner in ajax/jquery. I need to open a link in a new window like "target=_blank" but my code only makes the link to open in the same window. I have tried different things like inserting target _blank inside the jquery but it didn't work at all. I really appreciate any help [:)
function initHeroSwitch() {
var heroCnt = $('#hero ul li').size();
if ($('#hero').length > 0) {
$('#hero').append('<div id="hero-btns"></div>');
$('#hero ul li').each(function (index) {
$('#hero-btns').append(''); });
$('#hero ul li').each(function () {
getRel = $(this).attr('rel');
winHeight = $(window).height();
if (winHeight > 750) {
var bkgImg = getRel.split('.');
bkgImg = bkgImg[0];
bkgImg = bkgImg.substr(0, bkgImg.length - 3);
bkgImg = bkgImg + "-lg.jpg";
$('#hero ul li').each(function () { preload(bkgImg); });
} else {
preload(getRel);
}
});
startHeroRotate();
rotateHero(rotateSpeed);
} else { autoRotate = window.setInterval(function () { startNewsRotation(); }, rotateSpeed); }
}
Hvae you tried this:
$('#hero-btns').append('');