Rectangle 27 197

If you are using jQuery:

$.inArray(5 + 5, [ "8", "9", "10", 10 + "" ]);

Note that "inArray" is a misnomer, because it doesn't return boolean value - it returns index of first element found. So if you are checking if element exists you should use if (-1 != $.inArray(...)) ....

Helpful, but I don't think it's an appropriate answer here. The question is tagged "javascript" indicating "vanilla" in my opinion. :)

Best way to find if an item is in a JavaScript array? - Stack Overflow

javascript arrays
Rectangle 27 196

If you are using jQuery:

$.inArray(5 + 5, [ "8", "9", "10", 10 + "" ]);

Note that "inArray" is a misnomer, because it doesn't return boolean value - it returns index of first element found. So if you are checking if element exists you should use if (-1 != $.inArray(...)) ....

Helpful, but I don't think it's an appropriate answer here. The question is tagged "javascript" indicating "vanilla" in my opinion. :)

Best way to find if an item is in a JavaScript array? - Stack Overflow

javascript arrays
Rectangle 27 37

If you are using jQuery then:

<a id="openMap" href="/map/">link</a>

This has the benefit of still working without JS, or if the user middle clicks the link.

It also means that I could handle generic popups by rewriting again to:

<a class="popup" href="/map/">link</a>

This would let you add a popup to any link by just giving it the popup class.

<a class="popup" data-width="300" data-height="300" href="/map/">link</a>
$(document).ready(function() {
    $(".popup").click(function(){
        popup($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');
        return false;
    });
});

I can now use the same bit of code for lots of popups on my whole site without having to write loads of onclick stuff! Yay for reusability!

It also means that if later on I decide that popups are bad practice, (which they are!) and that I want to replace them with a lightbox style modal window, I can change:

popup($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');
myAmazingModalWindow($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');

and all my popups on my whole site are now working totally differently. I could even do feature detection to decide what to do on a popup, or store a users preference to allow them or not. With the inline onclick, this requires a huge copy and pasting effort.

Works without JavaScript?? It's jQuery. It needs Javascript enabled on the browser to work, right?

Yes, but the link can redirect to a page performing the action without JavaScript in this case.

The link works without JavaScript. See how the link has a normal href attribute. If the user doesn't have JavaScript the link will still be a link and the user can still access the content.

@Thomas Shields: No, he means that if you don't have Javascript, the link will still take you to /map/...

javascript - Why is using onClick() in HTML a bad practice? - Stack Ov...

javascript html
Rectangle 27 37

If you are using jQuery then:

<a id="openMap" href="/map/">link</a>

This has the benefit of still working without JS, or if the user middle clicks the link.

It also means that I could handle generic popups by rewriting again to:

<a class="popup" href="/map/">link</a>

This would let you add a popup to any link by just giving it the popup class.

<a class="popup" data-width="300" data-height="300" href="/map/">link</a>
$(document).ready(function() {
    $(".popup").click(function(){
        popup($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');
        return false;
    });
});

I can now use the same bit of code for lots of popups on my whole site without having to write loads of onclick stuff! Yay for reusability!

It also means that if later on I decide that popups are bad practice, (which they are!) and that I want to replace them with a lightbox style modal window, I can change:

popup($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');
myAmazingModalWindow($(this).attr("href"), $(this).data('width'), $(this).data('height'), 'map');

and all my popups on my whole site are now working totally differently. I could even do feature detection to decide what to do on a popup, or store a users preference to allow them or not. With the inline onclick, this requires a huge copy and pasting effort.

Works without JavaScript?? It's jQuery. It needs Javascript enabled on the browser to work, right?

Yes, but the link can redirect to a page performing the action without JavaScript in this case.

The link works without JavaScript. See how the link has a normal href attribute. If the user doesn't have JavaScript the link will still be a link and the user can still access the content.

@Thomas Shields: No, he means that if you don't have Javascript, the link will still take you to /map/...

javascript - Why is using onClick() in HTML a bad practice? - Stack Ov...

javascript html
Rectangle 27 67

If you are using jQuery < 1.6 do this:

jQuery("input[type='text']").attr("disabled", 'disabled');

If you are using jQuery 1.6+:

jQuery("input[type='text']").prop("disabled", true);

Or you can try this:

$('input:text').attr("disabled", 'disabled');
:text

$("input:text").attr("disabled", "disabled");. Just pasting that in to check if I have the same code, heh. Can't get it to work. Jeez, stupid Chrome. :(

@Cabaret, can you post an example (working) code on jsfiddle.net and see if you get the same results?

Yeah so.. I don't get it. It works fine in JSfiddle and it doesn't work in my application. jsfiddle.net/b6L6g/1

jQuery .attr("disabled", "disabled") not working in Chrome - Stack Ove...

jquery google-chrome cross-browser attr jquery-attributes
Rectangle 27 67

If you are using jQuery < 1.6 do this:

jQuery("input[type='text']").attr("disabled", 'disabled');

If you are using jQuery 1.6+:

jQuery("input[type='text']").prop("disabled", true);

Or you can try this:

$('input:text').attr("disabled", 'disabled');
:text

$("input:text").attr("disabled", "disabled");. Just pasting that in to check if I have the same code, heh. Can't get it to work. Jeez, stupid Chrome. :(

@Cabaret, can you post an example (working) code on jsfiddle.net and see if you get the same results?

Yeah so.. I don't get it. It works fine in JSfiddle and it doesn't work in my application. jsfiddle.net/b6L6g/1

jQuery .attr("disabled", "disabled") not working in Chrome - Stack Ove...

jquery google-chrome cross-browser attr jquery-attributes
Rectangle 27 104

You can do this by using JQuery.

<a href="your URL" id="navigate"> click here </a>
JQuery
$(document).ready(function(){

    $('a').on('mousedown', stopNavigate);

    $('a').on('mouseleave', function () {
           $(window).on('beforeunload', function(){
                  return 'Are you sure you want to leave?';
           });
    });
});

function stopNavigate(){    
    $(window).off('beforeunload');
}
$(window).on('beforeunload', function(){
      return 'Are you sure you want to leave?';
});

$(window).on('unload', function(){

         logout();

});

This solution works in all browsers and I have tested it.

This is a good one (you probably should observe onsubmit too), but it won't work for browser's Back and Forward buttons. I anticipate there is no ideal solution to this.

code is fine. I want to run logout() function if user clicks on "Leave this Page" pop button. But how can I detect whether user have clicked on "Leave this Page" or not?

@Bit_hunter use a confirm() dialog

$(window).on('unload', function()

Will beforeunload fire if the browser is closing due to OS shutdown?

javascript - jquery beforeunload when closing (not leaving) the page? ...

javascript jquery
Rectangle 27 1014

You could do this various different ways. It could be a subtle as a small status on the page saying "Loading...", or as loud as an entire element graying out the page while the new data is loading. The approach I'm taking below will show you how to accomplish both methods.

Let's start by getting us a nice "loading" animation from http://ajaxload.info I'll be using

Let's create an element that we can show/hide anytime we're making an ajax request:

<div class="modal"><!-- Place at bottom of page --></div>
/* Start by setting display:none to make this hidden.
   Then we position it in relation to the viewport window
   with position:fixed. Width, height, top and left speak
   for themselves. Background we set to 80% white with
   our animation centered, and no-repeating */
.modal {
    display:    none;
    position:   fixed;
    z-index:    1000;
    top:        0;
    left:       0;
    height:     100%;
    width:      100%;
    background: rgba( 255, 255, 255, .8 ) 
                url('http://i.stack.imgur.com/FhHRx.gif') 
                50% 50% 
                no-repeat;
}

/* When the body has the loading class, we turn
   the scrollbar off with overflow:hidden */
body.loading {
    overflow: hidden;   
}

/* Anytime the body has the loading class, our
   modal element will be visible */
body.loading .modal {
    display: block;
}
$body = $("body");

$(document).on({
    ajaxStart: function() { $body.addClass("loading");    },
     ajaxStop: function() { $body.removeClass("loading"); }    
});

That's it! We're attaching some events to the body element anytime the ajaxStart or ajaxStop events are fired. When an ajax event starts, we add the "loading" class to the body. and when events are done, we remove the "loading" class from the body.

This is the most in depth solution although I'd recommend you use a centering plugin which centers the preloader around a page element (i.e. body, #element, or .element)

I had to use .bind() instead of .on() as we are using jQuery 1.5.1!

I love when something just works. There's a reason @JonathanSampson has 68k rep.

I'd recommend using the plugin that plugins to the plugins to make sure they're all plugged in.

How can I create a "Please Wait, Loading..." animation using jQuery? -...

jquery animation
Rectangle 27 999

You could do this various different ways. It could be a subtle as a small status on the page saying "Loading...", or as loud as an entire element graying out the page while the new data is loading. The approach I'm taking below will show you how to accomplish both methods.

Let's start by getting us a nice "loading" animation from http://ajaxload.info I'll be using

Let's create an element that we can show/hide anytime we're making an ajax request:

<div class="modal"><!-- Place at bottom of page --></div>
/* Start by setting display:none to make this hidden.
   Then we position it in relation to the viewport window
   with position:fixed. Width, height, top and left speak
   for themselves. Background we set to 80% white with
   our animation centered, and no-repeating */
.modal {
    display:    none;
    position:   fixed;
    z-index:    1000;
    top:        0;
    left:       0;
    height:     100%;
    width:      100%;
    background: rgba( 255, 255, 255, .8 ) 
                url('http://i.stack.imgur.com/FhHRx.gif') 
                50% 50% 
                no-repeat;
}

/* When the body has the loading class, we turn
   the scrollbar off with overflow:hidden */
body.loading {
    overflow: hidden;   
}

/* Anytime the body has the loading class, our
   modal element will be visible */
body.loading .modal {
    display: block;
}
$body = $("body");

$(document).on({
    ajaxStart: function() { $body.addClass("loading");    },
     ajaxStop: function() { $body.removeClass("loading"); }    
});

That's it! We're attaching some events to the body element anytime the ajaxStart or ajaxStop events are fired. When an ajax event starts, we add the "loading" class to the body. and when events are done, we remove the "loading" class from the body.

This is the most in depth solution although I'd recommend you use a centering plugin which centers the preloader around a page element (i.e. body, #element, or .element)

I had to use .bind() instead of .on() as we are using jQuery 1.5.1!

I love when something just works. There's a reason @JonathanSampson has 68k rep.

I'd recommend using the plugin that plugins to the plugins to make sure they're all plugged in.

How can I create a "Please Wait, Loading..." animation using jQuery? -...

jquery animation
Rectangle 27 994

You could do this various different ways. It could be a subtle as a small status on the page saying "Loading...", or as loud as an entire element graying out the page while the new data is loading. The approach I'm taking below will show you how to accomplish both methods.

Let's start by getting us a nice "loading" animation from http://ajaxload.info I'll be using

Let's create an element that we can show/hide anytime we're making an ajax request:

<div class="modal"><!-- Place at bottom of page --></div>
/* Start by setting display:none to make this hidden.
   Then we position it in relation to the viewport window
   with position:fixed. Width, height, top and left speak
   for themselves. Background we set to 80% white with
   our animation centered, and no-repeating */
.modal {
    display:    none;
    position:   fixed;
    z-index:    1000;
    top:        0;
    left:       0;
    height:     100%;
    width:      100%;
    background: rgba( 255, 255, 255, .8 ) 
                url('http://i.stack.imgur.com/FhHRx.gif') 
                50% 50% 
                no-repeat;
}

/* When the body has the loading class, we turn
   the scrollbar off with overflow:hidden */
body.loading {
    overflow: hidden;   
}

/* Anytime the body has the loading class, our
   modal element will be visible */
body.loading .modal {
    display: block;
}
$body = $("body");

$(document).on({
    ajaxStart: function() { $body.addClass("loading");    },
     ajaxStop: function() { $body.removeClass("loading"); }    
});

That's it! We're attaching some events to the body element anytime the ajaxStart or ajaxStop events are fired. When an ajax event starts, we add the "loading" class to the body. and when events are done, we remove the "loading" class from the body.

This is the most in depth solution although I'd recommend you use a centering plugin which centers the preloader around a page element (i.e. body, #element, or .element)

I had to use .bind() instead of .on() as we are using jQuery 1.5.1!

I love when something just works. There's a reason @JonathanSampson has 68k rep.

I'd recommend using the plugin that plugins to the plugins to make sure they're all plugged in.

How can I create a "Please Wait, Loading..." animation using jQuery? -...

jquery animation
Rectangle 27 44

If you are using jQuery, you can do:

$("a").click(function (event) {
    event.preventDefault();
    window.location = $(this).attr("href");
});

Please explain why .live() might be better?

live will bind the event to all links including those that don't exist yet, click will only bind to ones that currently exist

thanks! lifesaver. I just spent hours trying to figure out why safari was loading all the time.

+1 from me - used this.href rather than casting to a jQuery object, but thanks for this answer. Works on iOS6.

.live() is deprecated as of jQuery 1.7, and removed as of 1.9. Use $(document).on('click', 'a', function(){...}) instead.

iPhone Safari Web App opens links in new window - Stack Overflow

iphone safari iphone-standalone-web-app
Rectangle 27 43

If you are using jQuery, following code will work for you.

$('input[name=radioName]:checked').val();

javascript - How to get the selected radio button value using js - Sta...

javascript radio-button
Rectangle 27 12

If you are using jQuery :

$(td).attr("title", "One \n Two \n Three");

tested in IE-9 : working.

Worked in MVC 3 Example: @Html.DropDownList("RegId", null, "Select Region", new { title = "Select a region from the drop-down menu, \nwhich is required to get a list of valid contract names", @class = "form-control", onchange = "getContractNames(this)", style = "width: 200px;" })

title - How can I use a carriage return in a HTML tooltip? - Stack Ove...

html title tooltip
Rectangle 27 12

If you are using jQuery :

$(td).attr("title", "One \n Two \n Three");

tested in IE-9 : working.

Worked in MVC 3 Example: @Html.DropDownList("RegId", null, "Select Region", new { title = "Select a region from the drop-down menu, \nwhich is required to get a list of valid contract names", @class = "form-control", onchange = "getContractNames(this)", style = "width: 200px;" })

title - How can I use a carriage return in a HTML tooltip? - Stack Ove...

html title tooltip
Rectangle 27 12

If you are using jQuery :

$(td).attr("title", "One \n Two \n Three");

tested in IE-9 : working.

Worked in MVC 3 Example: @Html.DropDownList("RegId", null, "Select Region", new { title = "Select a region from the drop-down menu, \nwhich is required to get a list of valid contract names", @class = "form-control", onchange = "getContractNames(this)", style = "width: 200px;" })

title - How can I use a carriage return in a HTML tooltip? - Stack Ove...

html title tooltip
Rectangle 27 12

if you are using jquery 1.7

$('html').off('click');
$('html').unbind('click');

attributes - How to remove "onclick" with JQuery? - Stack Overflow

jquery attributes
Rectangle 27 12

if you are using jquery 1.7

$('html').off('click');
$('html').unbind('click');

attributes - How to remove "onclick" with JQuery? - Stack Overflow

jquery attributes
Rectangle 27 19

If you are using jQuery you can do this:

$(function() {

  var $list = $("#list");

  $list.children().detach().sort(function(a, b) {
    return $(a).text().localeCompare($(b).text());
  }).appendTo($list);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<ul id="list">
  <li>delta</li>
  <li>cat</li>
  <li>alpha</li>
  <li>cat</li>
  <li>beta</li>
  <li>gamma</li>
  <li>gamma</li>
  <li>alpha</li>
  <li>cat</li>
  <li>delta</li>
  <li>bat</li>
  <li>cat</li>
</ul>

Note that returning 1 and -1 (or 0 and 1) from the compare function is absolutely wrong.

javascript - How may I sort a list alphabetically using jQuery? - Stac...

javascript jquery dom sorting
Rectangle 27 10

if you are using jQuery < 1.6 do this:

$('#ctl00_ContentPlaceHolder1_RadUploadImage').attr("disabled", 'disabled');

If you are using jQuery 1.6+:

$('#ctl00_ContentPlaceHolder1_RadUploadImage').prop("disabled", true);

dear @Neal thanks - i learned something new / but my radupload inside that div is still working - i test my code with prop! so confused1

whats do u mean radupload inside that div? what does ur html look like? can you add that to your question?

my Radupload source contains one ul -> disabled="disabled" about it does not work too ... also li's inside that ul!

sure / just a moment

@Neal -> but why it works on IE?

jquery - disabled attribute does not work in firefox - for a div - Sta...

jquery html
Rectangle 27 15

Well, if you are using jQuery, it's simpler.

if ($.trim(val).length === 0){
   // string is invalid
}

Works for newline and tab too, whereas the regex examples above don't as they are only looking for the absence of anything but white space. Although, I'm sure someone with some regex knowledge could create a regex that would also include tab / newline in the search.

javascript - How can I check if string contains characters & whitespac...

javascript string whitespace