locked
Default text in the textbox RRS feed

  • Question

  • I want to use javascript code which can display some default text within a text box that will disappear when clicked on - much better than the users having to delete the default text and if user does't enter any text that text is displayed as the value of the textbox.

    I know the code in HTML, but how to use it in CRM.

    <INPUT type="text" value="Enter Default Text " onFocus="if(this.value == 'Enter Default Text ') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Enter Default Text';}" />

    Suggestions?
     
    Sunday, July 26, 2009 12:50 PM

Answers

  • I think before you assign the default value, you need to check whether the form is in create mode or alternatively only assign it when the field is empty.

    var myField = crmForm.all.new_field;

    if (myField.DataValue == null || myField.DataValue == "")
    {
    myField.DataValue = "Enter Default Text";
    }

    myField.onfocus = function A()
    {
      if(myField.DataValue == "Enter Default Text")
        myField.DataValue = "";
    }
    myField.onblur = function B()
    {
      if(myField.DataValue == null)
        myField.DataValue = "Enter Default Text";
    }

    Hope this helps

    • Marked as answer by arjunrules19 Sunday, July 26, 2009 7:04 PM
    Sunday, July 26, 2009 6:30 PM
  • /*
    Checkbox style Multi-Select Picklist
    author: Jim Wang @ January 2009
    http://jianwang.blogspot.com
    */
    
    // PL - the picklist attribute; PLV - used to save selected picklist values
    var PL = crmForm.all.new_picklist;
    var PLV = crmForm.all.new_picklistvalue;
    
    if( PL != null && PLV != null )
    {
      PL.style.display = "none";
      PLV.style.display = "none";
    
      // Create a DIV container
      var addDiv = document.createElement("<div style='overflow-y:auto; height:80px; border:1px #6699cc solid; background-color:#ffffff;' />");
      PL.parentNode.appendChild(addDiv);
    
      // Initialise checkbox controls
      for( var i = 1; i < PL.options.length; i++ )
      {
        var pOption = PL.options[i];
        if( !IsChecked( pOption.text ) )
          var addInput = document.createElement("<input type='checkbox' style='border:none; width:25px; align:left;' />" );
        else
          var addInput = document.createElement("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' />" );
    
        var addLabel = document.createElement( "<label />");
        addLabel.innerText = pOption.text;
    
        var addBr = document.createElement( "<br />"); //it's a 'br' flag
    
        PL.nextSibling.appendChild(addInput);
        PL.nextSibling.appendChild(addLabel);
        PL.nextSibling.appendChild(addBr);
      }
    
      // Check if it is selected
      function IsChecked( pText )
      {
        if(PLV.value != "")
        {
          var PLVT = PLV.value.split("||");
          for( var i = 0; i < PLVT.length; i++ )
          {
            if( PLVT[i] == pText )
              return true;
          }
        }
        return false;
      }
    
      // Save the selected text, this filed can also be used in Advanced Find
      crmForm.attachEvent( "onsave" , OnSave);
      function OnSave()
      {
        PLV.value = "";
        var getInput = PL.nextSibling.getElementsByTagName("input");
    
        for( var i = 0; i < getInput.length; i++ )
        {
          if( getInput[i].checked)
          {
            PLV.value += getInput[i].nextSibling.innerText + "||";
          }
        }
      }
    }
    


    Did you enable the event?
    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://mscrm.cn
    • Marked as answer by arjunrules19 Monday, July 27, 2009 10:35 AM
    Monday, July 27, 2009 7:59 AM
    Moderator

All replies

  • As you wish:


    var myField = crmForm.all.new_field;
    myField.DataValue = "Enter Default Text";
    
    myField.onfocus = function A()
    {
      if(myField.DataValue == "Enter Default Text")
        myField.DataValue = "";
    }
    myField.onblur = function B()
    {
      if(myField.DataValue == null)
        myField.DataValue = "Enter Default Text";
    }

    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://mscrm.cn
    • Marked as answer by arjunrules19 Sunday, July 26, 2009 5:45 PM
    • Unmarked as answer by arjunrules19 Sunday, July 26, 2009 5:54 PM
    Sunday, July 26, 2009 3:36 PM
    Moderator
  • Put the code to the OnLoad event of the entity, you need to change the new_field to your field name.

    Cheers,
    Jim
    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://mscrm.cn
    Sunday, July 26, 2009 3:37 PM
    Moderator
  • Hi Jim,

    By using the code given by you, I am able to set default text and on focus, the value is set to null. But when I write any other text in the textbox, It is not saving with the same value in the database, the value is still "Enter Default Text".

    Sunday, July 26, 2009 5:44 PM
  • I think before you assign the default value, you need to check whether the form is in create mode or alternatively only assign it when the field is empty.

    var myField = crmForm.all.new_field;

    if (myField.DataValue == null || myField.DataValue == "")
    {
    myField.DataValue = "Enter Default Text";
    }

    myField.onfocus = function A()
    {
      if(myField.DataValue == "Enter Default Text")
        myField.DataValue = "";
    }
    myField.onblur = function B()
    {
      if(myField.DataValue == null)
        myField.DataValue = "Enter Default Text";
    }

    Hope this helps

    • Marked as answer by arjunrules19 Sunday, July 26, 2009 7:04 PM
    Sunday, July 26, 2009 6:30 PM
  • Thanks Wael, the code is working perfectly fine.

    Sunday, July 26, 2009 7:07 PM


  • I followed the code provided in the following link:
    http://jianwang.blogspot.com/2009/01/crm-40-checkbox-style-multi-select.html

    But I am not getting the anything apart from a simple picklist and textbox attribute.

    Suggestions?
    Sunday, July 26, 2009 7:58 PM
  • /*
    Checkbox style Multi-Select Picklist
    author: Jim Wang @ January 2009
    http://jianwang.blogspot.com
    */
    
    // PL - the picklist attribute; PLV - used to save selected picklist values
    var PL = crmForm.all.new_picklist;
    var PLV = crmForm.all.new_picklistvalue;
    
    if( PL != null && PLV != null )
    {
      PL.style.display = "none";
      PLV.style.display = "none";
    
      // Create a DIV container
      var addDiv = document.createElement("<div style='overflow-y:auto; height:80px; border:1px #6699cc solid; background-color:#ffffff;' />");
      PL.parentNode.appendChild(addDiv);
    
      // Initialise checkbox controls
      for( var i = 1; i < PL.options.length; i++ )
      {
        var pOption = PL.options[i];
        if( !IsChecked( pOption.text ) )
          var addInput = document.createElement("<input type='checkbox' style='border:none; width:25px; align:left;' />" );
        else
          var addInput = document.createElement("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' />" );
    
        var addLabel = document.createElement( "<label />");
        addLabel.innerText = pOption.text;
    
        var addBr = document.createElement( "<br />"); //it's a 'br' flag
    
        PL.nextSibling.appendChild(addInput);
        PL.nextSibling.appendChild(addLabel);
        PL.nextSibling.appendChild(addBr);
      }
    
      // Check if it is selected
      function IsChecked( pText )
      {
        if(PLV.value != "")
        {
          var PLVT = PLV.value.split("||");
          for( var i = 0; i < PLVT.length; i++ )
          {
            if( PLVT[i] == pText )
              return true;
          }
        }
        return false;
      }
    
      // Save the selected text, this filed can also be used in Advanced Find
      crmForm.attachEvent( "onsave" , OnSave);
      function OnSave()
      {
        PLV.value = "";
        var getInput = PL.nextSibling.getElementsByTagName("input");
    
        for( var i = 0; i < getInput.length; i++ )
        {
          if( getInput[i].checked)
          {
            PLV.value += getInput[i].nextSibling.innerText + "||";
          }
        }
      }
    }
    


    Did you enable the event?
    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://mscrm.cn
    • Marked as answer by arjunrules19 Monday, July 27, 2009 10:35 AM
    Monday, July 27, 2009 7:59 AM
    Moderator
  • That's right, I forgot it!;-)
    Jim Wang - MVP Dynamics CRM - http://jianwang.blogspot.com, http://mscrm.cn
    Monday, July 27, 2009 8:03 AM
    Moderator
  • Thanks!! It works.
    Monday, July 27, 2009 10:34 AM