Category Archives: Java Scripts

It contains Java Scripts that I learn newly.

How to handle the resets on loss of focus while using autocomplete combobox in IE?

You can try using the jQuery UI autocomplete’s open and close event to control your textbox’s blur event. When the autocomplete is open you disable the blur event and when it close you enable your blur event again.

var disable=false;

$( "#tags" ).autocomplete({
  source: availableTags,
  open: function(event, ui) { disable=true },
  close: function(event, ui) {
    disable=false; $(this).focus();
  }
}).blur(function() {
  if(!disable) {
    alert('blur');
  }
});

Instead of

alert('blur');

write the code that you need to workout when exactly the focus of the input filed is lossing.

I personly tested and used it.

How can I get last characters of a string using JavaScript?

You’ll want to use the Javascript string method .substr() combined with the .length property.

var id = "ctl03_Tabs1";
var lastFive = id.substr(id.length - 5); // => "Tabs1"
var lastChar = id.substr(id.length - 1); // => "1"

This gets the characters starting at id.length – 5 and, since the second argument for .substr() is omitted, continues to the end of the string.

If you’re simply looking to find the characters after the underscore, you could use this:

var tabId = id.split("_").pop(); // => "Tabs1"

This splits the string into an array on the underscore and then “pops” the last element off the array (which is the string you want).

innerHTML not working in Internet Explorer(IE)

We may see that we don’t get proper output in IE when we try to use

document.getElementById('my_id').innerHTML="mm";

This mainly happens in the case we try to implement innerHTML into the select tag. The way I recommend to overcome this situation is to use jquery. We can write the content in jquery as follows:

$('#my_id').html("<option value='mm'>mm</option>");

I had tested it and saw that it’s working.

how to Select All / Deselect All options in multiple select box using java script?

It’s as follows :

 

function listbox_selectall(listID, isSelect) {
        var listbox = document.getElementById(listID);
        for(var count=0; count < listbox.options.length; count++) {
            listbox.options[count].selected = isSelect;
    }
}

 

onclick="listbox_selectall('countryList', true)" //select all the options

(or)

onclick="listbox_selectall('countryList', false)" //deselect all the options

how to get multiple selected values and items from listbox using javascript?

It’s as follows :

 

var src = document.getElementById('leftSelect');
var selected = [];
var options = src.options.length;
for (var i=0; i < options ; i++)
{
     if(src.options[i].selected)
     {
          alert(src.options[i].text);
          alert(src.options[i].value);
     }
}

 

<html>
<select id="leftSelect" onclick="" name="edu_quli" size="10" multiple="multiple">
<option value="10th">10th</option>
<option value="+2">+2</option>
<option value="Any Graduation">Any Graduation</option>
<option value="Any Post Graduation">Any Post Graduation</option>
<option value="Arts Post-Graduation">Arts Post-Graduation</option><option value="Science Post-Graduation">Science Post-Graduation</option>
<option value="Commerce Post-Graduation">Commerce Post-Graduation</option>
<option value="Technical/Professional Post-Graduation">Technical/Professional Post-Graduation</option>
<option value="Management Post-Graduation">Management Post-Graduation</option>
<option value="Other Post-Graduation">Other Post-Graduation</option>
<option value="Diploma">Diploma</option>
<option value="Others">Others</option>
</select>
</html>