0

I want to disable Date and Time picker but I can´t get input textbox, I always get undefined

Code

<script>

// Search for the input textbox of DatePicker by its title and get its Item ID 


var itemID = $(" input[title='Fechados'] ").attr("id");

// Trim the Item ID and keep the first part which is common for each element of this DatePicker

var itemCode = itemID.substring(0, itemID.indexOf('$'));

// Set text input to readOnly  

$( "[id^=" + itemCode + "] td.ms-dtinput > input[id$='Date']" ).attr('readonly', 'readonly');

// Disable the onclick action on the button

$( "[id^=" + itemCode + "] td.ms-dtinput > a" ).attr('onclick','').unbind('click');

// Disable the 2 time select elements

$( "[id^=" + itemCode + "] td.ms-dttimeinput > select[id$='DateHours']" ).attr('disabled', 'disabled');

$( "[id^=" + itemCode + "] td.ms-dttimeinput > select[id$='DateMinutes']" ).attr('disabled', 'disabled');

</script>

This line always throw me undefined:

 var itemID = $(" input[title='Fechados'] ").attr("id");

I don´t know why if Title of my column is correctly. Regards

Message received in console:

Uncaught TypeError: Cannot read property 'substring' of undefined

HTML:

<td class="ms-dtinput">
<label for="Fechados_fb10b3d7-d448-474d-af75-13c64038c3e5_$DateTimeFieldDate" style="display:none">Fechados Date</label>
<input type="text" value="31/08/2017" maxlength="45" id="Fechados_fb10b3d7-d448-474d-af75-13c64038c3e5_$DateTimeFieldDate" title="Fechados" class="ms-input" autopostback="0">
</td>
  • Could you show html containing field which id you are trying to get? – rafter13 Sep 1 '17 at 6:04
  • Yes, I added it to my question @rafter13 – Ledwing Sep 1 '17 at 6:08
0

You can get your input by using his id and jQuery $("#") selector. You also need to escape special character with \\ (See doc) :

$("#Fechados_fb10b3d7-d448-474d-af75-13c64038c3e5_\\$DateTimeFieldDate");

Then if you want to disable it you can append .attr("readonly", true);

  • So instead this line $(" input[title='Fechados'] ").attr("id"); use this? $("#Fechados_fb10b3d7-d448-474d-af75-13c64038c3e5_$DateTimeFieldDate").attr("readonly",true);? – Ledwing Sep 1 '17 at 6:38
  • Absolutely, using the id of an element is the best way to select it – RomainB Sep 1 '17 at 6:40
  • I drop all my script and use only $("#Fechados_fb10b3d7-d448-474d-af75-13c64038c3e5_$DateTimeF‌​ieldDate").attr("rea‌​donly",true); but I get Uncaught Error: Syntax error, unrecognized expression: #Fechados_fb10b3d7-d448-474d-af75-13c64038c3e5_$DateTimeF‌​ieldDate – Ledwing Sep 1 '17 at 6:41
  • Well, that's because you have a "$" in your id, see my edited post. – RomainB Sep 1 '17 at 6:53
  • If there are no more fields with the same title on the page, your selector based on 'title' attribute was just fine. Using Id which is generated by SP and completely unclear is not a better idea in my opinion. – rafter13 Sep 1 '17 at 7:15

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.