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>
2
  • Could you show html containing field which id you are trying to get? Commented Sep 1, 2017 at 6:04
  • Yes, I added it to my question @rafter13 Commented Sep 1, 2017 at 6:08

1 Answer 1

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);

5
  • So instead this line $(" input[title='Fechados'] ").attr("id"); use this? $("#Fechados_fb10b3d7-d448-474d-af75-13c64038c3e5_$DateTimeFieldDate").attr("readonly",true);? Commented Sep 1, 2017 at 6:38
  • Absolutely, using the id of an element is the best way to select it Commented Sep 1, 2017 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 Commented Sep 1, 2017 at 6:41
  • Well, that's because you have a "$" in your id, see my edited post. Commented Sep 1, 2017 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. Commented Sep 1, 2017 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.