Move up and down listbox item using javascript

     In the following example,we can easy to move the list box item up and down using javascript code.
Example

    function listboxMove(listID, direction) {

      var listbox = document.getElementById(listID);

      var selIndex = listbox.selectedIndex;

      if (-1 == selIndex) {

        alert("Please select an option to move.");

        return;

      }

      var increment = -1;

      if (direction == 'up')

        increment = -1;

      else

        increment = 1;

      if ((selIndex + increment) < 0 ||

        (selIndex + increment) > (listbox.options.length - 1)) {

        return;

      }

      var selValue = listbox.options[selIndex].value;

      var selText = listbox.options[selIndex].text;

      listbox.options[selIndex].value = listbox.options[selIndex + increment].value

      listbox.options[selIndex].text = listbox.options[selIndex + increment].text

      listbox.options[selIndex + increment].value = selValue;

      listbox.options[selIndex + increment].text = selText;

      listbox.selectedIndex = selIndex + increment;

    }


Example Program:- (Editor)


Editor is Loading...

Advertisement

Related Posts:

  • Math.min() in javascript     The Math.min() function returns the smallest of zero or more numbers. min() is a static method of Math, you always use it as Math.… Read More
  • toExponential() in javascript     The toExponential() method returns a string representing the Number object in exponential notation.ParametersfractionDigits  … Read More
  • toFixed() in javascript     The toFixed() method formats a number using fixed-point notation.digitsOptional. The number of digits to appear after the decimal … Read More
  • Math.max() in javascript     The Math.max() function returns the largest of zero or more numbers. max() is a static method of Math, you always use it as Math.m… Read More
  • toPrecision() in javascript     The toPrecision() method returns a string representing the Number object to the specified precision.     A string r… Read More

0 nhận xét:

Đăng nhận xét