locked
Quick javascript question RRS feed

  • Question

  • I'm trying to make a generic onchange script that references the field that the onchange was called from.

    So if normally I would put something like

    crmForm.all.contoso_parentfield.DataValue

    How can I reference the calling field without hardcoding it's name?

    crmForm.all[dynamicvalue].DataValue    somewhat like this.

    Thursday, January 27, 2011 2:12 PM

Answers

  • Hello Nick

     

    To get identifier of field which fired the handler ou can use following code:

    var id = event.srcElement.id;


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    • Marked as answer by nickpeterson Thursday, January 27, 2011 9:29 PM
    Thursday, January 27, 2011 2:50 PM
    Moderator
  • I had a quick read of your requirement, I don't believe I have 100% understanding. But I have done something similar which I called them dependent or cascading controls, in which case, I don't use CRM onchange event. Instead, I use JS attachEvent to trigger further downstream actions. 

    You may get the idea if you have a look at one of my blog post which I provided this kind of cascading behavior for country/province picklists. 

    JS has full potential to get your code generic enough for your requirement, I believe. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by nickpeterson Thursday, January 27, 2011 9:29 PM
    Thursday, January 27, 2011 3:59 PM

All replies

  • You should be able to do that, assuming that dynamicvalue is the quoted string of the field name which could be "contoso_parentfield" in you case.

    Did you run into any issue? 

     


    Daniel Cai | http://danielcai.blogspot.com
    Thursday, January 27, 2011 2:18 PM
  • How do I know the field name calling the onchange event unless I explicitly set it somewhere though. In other words, can I figure out the field that called the javascript that is currently executing.

    Thursday, January 27, 2011 2:21 PM
  • You have the local variable called dynamicvalue which is the field that you are changing. Also, if you attach a piece of code to this field's onchange event, you should still be able to find out the firing field in that piece of code. 


    Daniel Cai | http://danielcai.blogspot.com
    Thursday, January 27, 2011 2:25 PM
  • Hi, try this:

    function onCha(){
    // DO SOMETHING 
    }

     for (var index = 0; index < crmForm.all.length; index++)
         {
              crmForm.all[index].attachEvent('onclick', onCha);
       }

     


    Julio
    Thursday, January 27, 2011 2:26 PM
  • I guess the reason my question is odd is because I'm trying to do something odd. I'm making an entity to manage dependent picklists. I've setup an entity with several fields

    • parententity
    • parentfield
    • parentvalue
    • childfield
    • childvalue
    • childdatavalue

    The thought is that people can go in and add a record, that allows them to map a relationship between a parent and child picklist.

    Then on the onload and onchange events of certain entities, there will be a stock piece of javascript that fires and configures the dynamic picklists.

    I was hoping to make it very general, to the point that when someone adds a new record to the crm, I would have a custom piece of code (not sure if I would use a plugin or onsave javascript webservice call) that places a function call in the onchange event of the field in question.

    I was wondering if I could make that function truly generic and have it figure out its own name without feeding it a parameter with the fieldname, but it occurs to me upon writing this out that I can use the childfield value from the crm record in question to place the javascript in the right place. Sorry for the confusion if this wasted anyones time.

    Thursday, January 27, 2011 2:37 PM
  • Hello Nick

     

    To get identifier of field which fired the handler ou can use following code:

    var id = event.srcElement.id;


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    • Marked as answer by nickpeterson Thursday, January 27, 2011 9:29 PM
    Thursday, January 27, 2011 2:50 PM
    Moderator
  • I had a quick read of your requirement, I don't believe I have 100% understanding. But I have done something similar which I called them dependent or cascading controls, in which case, I don't use CRM onchange event. Instead, I use JS attachEvent to trigger further downstream actions. 

    You may get the idea if you have a look at one of my blog post which I provided this kind of cascading behavior for country/province picklists. 

    JS has full potential to get your code generic enough for your requirement, I believe. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by nickpeterson Thursday, January 27, 2011 9:29 PM
    Thursday, January 27, 2011 3:59 PM