sapdev logo background
sapdev logo sapdev logo

Useful ABAP, HTML, Javascript, CSS code for your SAP BSP applications

--The javascript getElementById statement
See here for example syntax using the getElementById command

--Add multiple classes to an HTML element
This is fairly straight forward simply enter both classes with a space between
class="cssclass1 cssclass2"

--Stop Enter key triggering event
onkeypress="return event.keyCode != 13"

--Stop OnInputProcessing event being triggered when pressing enter
This code is to fix an issue I often have where pressing enter within an html input field triggers the submit form/button. You could use the code above to stop all enter functionality happening on each field but sometimes you may want javascript code to run on enter which updates other screen fields etc.

One trick I have found to do this is to have a second hidden button which is before the main submit button so that it is the default. You can then give this button a separate event_id so that you know the user was simply pressing enter on a field rather than pressing the visible submit button. All you have to do then is capture all the screen fields and re-display it as if nothing as happened.

Here is the html code to hide a button on a white screen
<input style="background-color:#ffffff; border:0;" name="OnInputProcessing(enterpressed)" type="submit" value="" id="enterpressed">

Then in your "OnInputProcessing" event just add the following code
IF event_id EQ 'enterpressed'.
* Capture all fields on screen save them and then do nothing to
* return user to to screen as if nothing happened

--Replace Enter press event with Tab
A much simpler solution to stop a user pressing enter and firing the submit buton(OnInputProcessing) would be to turn the enter key pressed action into a tab, by adding the following code.

The following code works in IE, chrome and firefox although you may need to add tabindex attributes to some of your elements. The code simply gets placed within the <head> tag of your page as shown below. No other code is required to initiate it as it adds the functionality to a keydown listener so that whenever a keydown action is performed this code gets fired.

The code then checks if the action was an Enter press (keyCode == 13), then in IE it changes it to a tab event (keyCode = 9) and in chrome it targets the next element in the tabindex.

Please note having fields within a <fieldset> or hidden can stop this working in Chrome so if it doesnt work check this first. See here for very simple page which demonstrates Enter key to Tab key working. You could create this page as a BSP and check that the OnInputProcessing doesnt get triggered.

<script type="text/javascript">
      window.onload = function()
        var form = document.getElementById("formlist");
        if (window.addEventListener)
          form.addEventListener("keydown", function(e)
             if (e.keyCode == 13) {
          }, true);
        else if (window.attachEvent)
          form.attachEvent("onkeydown", function()
            if (window.event.keyCode == 13)
              return window.event.keyCode = 9;

This is an alternative way of chnaging the Enter functionality but this version only seems to work in IE. Within chrome it captures the Enter event but fails to turn it into a tab event.

First of all add the onkeydown attribute to your body tag and then add the second peice of code within your <head> tag
<body onkeydown="ModifyEnterKeyPressAsTab(event);">

<script language="JavaScript">
function ModifyEnterKeyPressAsTab(e)
  e = e || window.event;

  if (e.keyCode == 13){
     if (window.event)
       e.keyCode = 9;

--Add select all checkboxes option to HTML page

Add the following javascript code between your head tags  
      <script language="JavaScript">
         function processAll(source) {
           checkboxes = document.getElementsByTagName("input");
           for(var i=0, n=checkboxes.length;i<n;i++) {
           checkboxes[i].checked = source.checked;

Add the following html code wher you want the option to appear  

<input type="checkbox" onClick="processAll(this);" /> Select/Unselect All

--Add HTML code to DIV section using javascript code
This could also be added to a js function that could be called from events such as onclick, onchange etc

 <script type="text/javascript">
        var dl = 'hello';

        document.getElementById("display_location").innerHTML = ' ';
        var f = document.createElement("div");
        f.innerHTML = dl;

--Call one or more Javascript functions from HTML element event (onchange, onclick...)
<input onchange="getTest();getTest2();" id="test_id" name="test_id" type="text" value="test"/>

--Call Javascript code from HTML element event (onchange, onclick...)
<input onchange="alert('hello world');" id="test_id" name="test_id" type="text" value="test"/>


--Capture row selected in HTML select/option field
First add html to create select dropdown field

<select id="dropdownmenu" name="dropdownmenu" onchange="getSelectedRow(this);">
<option value="" selected="selected">Choose Investigator</option>
<option id="1" value="value1"> Text1</option>
<option id="2" value="value2"> Text2</option>
<option id="3" value="value3"> Text3</option>

Then add the following javascript code

function getSelectedRow( v_this )
var line = v_this.selectedIndex;

if (v_this.getElementsByTagName("OPTION")[line].id == '1')
alert('Row 1 selected');

if (v_this.getElementsByTagName("OPTION")[line].id == '2')
alert('Row 2 selected');

if (v_this.getElementsByTagName("OPTION")[line].id == '3')
alert('Row 3 selected');

An alternative way without passing the 'this' option so do not know which option is selected yet

function getSelectedRow( )
for ( var i = 0; i < document.getElementById('dropdownmenu').options.length; i++ ) {
if ( document.getElementById('dropdownmenu').options[i].value == document.getElementById('dropdownmenu').value ) {
var usertype = document.getElementById('dropdownmenu').options[i].id;

--Set Dropdown value in chosen.js Jquery addon
Where 'document.getElementById('field1')' = the dropdown field to be updated
and 'Value1' is the value to set it too

setSelectedValue(document.getElementById('field1'), 'Value1');

function setSelectedValue(s, v) {
  for ( var i = 0; i < s.options.length; i++ ) {
    if ( s.options[i].value == v ) {
/*    sets selected dropdown row */
      s.options[i].selected = true;

--Display value with specific number of decimal values (.toFixed)

data:    value1     type string,
         value2     type string,
         totalvalue type string,

value1     = 'unitcost1'.  "ID attribute of field
value2     = 'numunits1'.  "ID attribute of field
totalvalue = 'tot1'.       "ID attribute of field

var num =
   document.getElementById("<%=value1%>").value * document.getElementById("<%=value2%>").value;
   document.getElementById("<%=totalvalue%>").value = num.toFixed(2);

--Disable 'right mouse click' on your SAP BSP
Simply add the following javascript code to your HTML BSP, within the head tags


<script type="text/javascript">
        return false;


--JQuery example code based on the jquery chosen dropdown functionality

$('div[id="location_chzn"]').attr('readonly', 'readonly');  	/*set field as readonly*/

$('div[id="location_chzn"]').attr('readonly', ''); /*remove readonly attribute*/
$('div[id="location_chzn"]').attr('disabled', 'disabled'); /*set field as disabled*/
$('div[id="location_chzn"]').removeAttr('disabled'); /*remove disabled attribute*/

--Style <INPUT> tag with image

<input type="submit" name="OnInputProcessing(getexcel)" class="btn-save" value="Save" >

CSS styling code:

input.btn-save {
  height: 36px;
  padding: 2px 5px 2px 26px;
  margin-top: 0px;
  border: 1px solid #afafaf;
  font-weight: bold;
  font-size: 12px;
  cursor: pointer;
  background: url(../img/save.png) left no-repeat #e4e4e4;
  -webkit-border-radius: 3px;
     -moz-border-radius: 3px;
          border-radius: 3px;

input.btn-save:hover {
  border: 1px solid #004284;
  color: #004284;

input.btn-save:active {
    position: relative;
    top: 1px;
    border: 1px solid red;
    color: red;

--Capture HTML code of current page
Call from onclick event and stores html code into hidden input field on page. Can then capture this within the OnInputProcessing BSP event

html code: <input onclick="exportHtml();" type="submit" name="OnInputProcessing(gethtml)" value="Export to file" >
<input type="hidden" name="storehtml" id="storehtml" />

Javascript code:

<script type="text/javascript">
  function exportHtml() {
     var storecode = document.getElementsByTagName('body')[0].innerHTML;
     document.getElementById('storehtml').value = storecode;

--Capture selected radiobutton using javascrip

HTML Code:
<input type="radio" id="sos_staff" name="sos" value="Staff">Staff
<input type="radio" id="sos_student" name="sos" value="Student">Student

Javascript code:
if(document.getElementById('sos_staff').checked) {

}else if(document.getElementById('sos_student').checked) {


comments powered by Disqus