0

I am trying to retrieve the value of Textbox and dropdownlist in NewForm.aspx using jQuery and JavaScript when PreSaveaction is called. Below is my code: But both cases, I am not able to get values for validation.
What am I doing wrong ?

     <script language="javascript" src="/project/AssetsLib/jquery-
    1.6.0.min.js" type="text/javascript"></script><script 
    language="javascript" src="/project/AssetsLib/jquery.SPServices-
    2014.01.min.js" type="text/javascript"></script><script 
     language="javascript" type="text/javascript">

      var $j = jQuery.noConflict();

       function PreSaveAction()
     { 
     alert("inside presaveaction ..........");

     var empNummm = $j('input[title="Opportunity ID"]').val();

      alert(' oppur id new is === ' + empNummm); //here its "undefined"
       var txtopportunID =     document.getElementById('ctl00_m_g_4f93017e_9d83_4446_8d75_438b3be4fb68_ctl00_ctl05_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField').value;
   alert(txtopportunID + ' ===is the opportunity id');

    var opportypeobj =               document.getElementById('ctl00_m_g_4f93017e_9d83_4446_8d75_438b3be4fb68_ctl00_ctl05_ctl05_ctl00_ctl00_ctl04_ctl00_DropDownChoice');
     alert(opportypeobj);

     }
      </script>
  • Are you absolutely certain your field names and IDs are correct? Is Opportunity ID definitely the Title attribute of the input field? Double check this with your browser's developer tools (hit F12). – Submits Oct 9 '15 at 7:49
  • Using the full ID is a bad pracice. Since you are using jQuery anyway, try it like this: $("input[id*='Idofelement']"). – Akhoy Oct 9 '15 at 8:39
  • the below code for the oppor.id , I took it from F12, developertools in my IE browser : <input name="ctl00$m$g_4f93017e_9d83_4446_8d75_438b3be4fb68$ctl00$ctl05$ctl00$ctl00$ctl00$ctl04$ctl00$ctl00$TextField" title="Opportunity ID Required Field" class="ms-long ms-spellcheck-true" id="ctl00_m_g_4f93017e_9d83_4446_8d75_438b3be4fb68_ctl00_ctl05_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField" type="text" maxLength="11" value="TBC"/> – userAZLogicApps Oct 9 '15 at 10:41
  • Submits - your are correct , the internal name of field opportunity id is Title .So should I mention it as the display name or the internal name of the field. – userAZLogicApps Oct 9 '15 at 10:44
  • one more question, if in the column settings, the column validation formula is written already, will this affect the jQuery/javascript validation? I mean, if the jQuery validation and default column validation- by applying formula - can co-exist? – userAZLogicApps Oct 9 '15 at 12:00
3

Seeing your comment, your selector should be: Explicit, matching the actual ID,

$("input[Title='Opportunity ID Required Field']").val()

A little looser, title contains opprotunity ID,

$("input[Title*='Opportunity ID']").val()

See http://sympmarc.com/2014/01/23/office-365-update-changes-display-name-on-required-fields/

| improve this answer | |
  • If its the default NewForm, it could be better to use the InternalName. It can be found by using the comment they add in the HTML over the TR. Then can users change the DisplayName without breaking the code. – Anders Aune Oct 9 '15 at 14:21
1

For dropdown purpose use,

jQuery("select[title='ColumnName'] option:selected").text();

And for text field use,

jQuery('input[title="ColumnName"]').val();

instead of using $ object use jQuery, it specifies the javascript object while $ specifies it as a reference variable.

| improve this answer | |
0

Put your jQuery code in

$(document).ready(function()
{    
    //Your code
});
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.