Why does this input always remain clickable? - javascript

Basically i want the outer div to take all events, so the input and anything else in the div is not clickable:
<div id="div1">
<div id="div2">
<label>Input 1</label>
<input type="text" id="input1" />
</div>
</div>
$(document).ready(function() {
$(document).on('click', '#input1', function(e){
e.preventDefault();
};
});
Here's the non-working example http://jsfiddle.net/KFWmk/3/
Any help appreciated :)

In newer jQuery (1.6+):
$('div input').prop("readonly", true);
This sets the HTML readonly property to true for all inputs in a div.
Please note that using .attr() to set properties (attributes with boolean values of on or off) has been deprecated in jQuery.

First of all bacause you havn't included the jQuery library.
Second bacause you have a type-o.
Third because the the focus is emitted before the click event. (Prove)
If you want to make an input field not editable you can use:
$("input").prop("readonly", true);
or simply when you create the input field in html:
<input readonly type="number">
If you want to prevent focus on an input field you can use:
$("input").on("focus", function() {
$(this).blur();
});

$('div input').attr("readonly", "readonly");
you can make all input fields in the div as read only.

You can try like this
$(document).ready(function() {
$('div input').prop("readonly", true);
});

Related

Bootstrap datepicker, autofill to and disable typing on select date [duplicate]

How do I use the jQuery Datepicker with a textbox input:
$("#my_txtbox").datepicker({
// options
});
that doesn't allow the user to input random text in the textbox.
I want the Datepicker to pop up when the textbox gains focus or the user clicks on it, but I want the textbox to ignore any user input using the keyboard (copy & paste, or any other). I want to fill the textbox exclusively from the Datepicker calendar.
Is this possible?
jQuery 1.2.6
Datepicker 1.5.2
You should be able to use the readonly attribute on the text input, and jQuery will still be able to edit its contents.
<input type='text' id='foo' readonly='true'>
Based on my experience I would recommend the solution suggested by Adhip Gupta:
$("#my_txtbox").attr( 'readOnly' , 'true' );
The following code won't let the user type new characters, but will allow them to delete characters:
$("#my_txtbox").keypress(function(event) {event.preventDefault();});
Additionally, this will render the form useless to those who have JavaScript disabled:
<input type="text" readonly="true" />
try
$("#my_txtbox").keypress(function(event) {event.preventDefault();});
If you are reusing the date-picker at multiple places then it would be apt to modify the textbox also via JavaScript by using something like:
$("#my_txtbox").attr( 'readOnly' , 'true' );
right after/before the place where you initialize your datepicker.
<input type="text" readonly="true" />
causes the textbox to lose its value after postback.
You rather should use Brad8118's suggestion which is working perfectly.
$("#my_txtbox").keypress(function(event) {event.preventDefault();});
EDIT:
to get it working for IE use 'keydown' instead of 'keypress'
After initialising the date picker:
$(".project-date").datepicker({
dateFormat: 'd M yy'
});
$(".project-date").keydown(false);
$("#txtfromdate").datepicker({
numberOfMonths: 2,
maxDate: 0,
dateFormat: 'dd-M-yy'
}).attr('readonly', 'readonly');
add the readonly attribute in the jquery.
You have two options to get this done. (As far as i know)
Option A: Make your text field read only. It can be done as follows.
Option B: Change the curser onfocus. Set ti to blur. it can be done by setting up the attribute onfocus="this.blur()" to your date picker text field.
Ex: <input type="text" name="currentDate" id="currentDate" onfocus="this.blur()" readonly/>
To datepicker to popup on gain focus:
$(".selector").datepicker({ showOn: 'both' })
If you don't want user input, add this to the input field
<input type="text" name="date" readonly="readonly" />
I just came across this so I am sharing here. Here is the option
https://eonasdan.github.io/bootstrap-datetimepicker/Options/#ignorereadonly
Here is the code.
HTML
<br/>
<!-- padding for jsfiddle -->
<div class="input-group date" id="arrival_date_div">
<input type="text" class="form-control" id="arrival_date" name="arrival_date" required readonly="readonly" />
<span class="input-group-addon">
<span class="glyphicon-calendar glyphicon"></span>
</span>
</div>
JS
$('#arrival_date_div').datetimepicker({
format: "YYYY-MM-DD",
ignoreReadonly: true
});
Here is the fiddle:
http://jsfiddle.net/matbaric/wh1cb6cy/
My version of bootstrap-datetimepicker.js is 4.17.45
I know this thread is old, but for others who encounter the same problem, that implement #Brad8118 solution (which i prefer, because if you choose to make the input readonly then the user will not be able to delete the date value inserted from datepicker if he chooses) and also need to prevent the user from pasting a value (as #ErikPhilips suggested to be needed), I let this addition here, which worked for me:
$("#my_txtbox").bind('paste',function(e) { e.preventDefault(); //disable paste }); from here https://www.dotnettricks.com/learn/jquery/disable-cut-copy-and-paste-in-textbox-using-jquery-javascript
and the whole specific script used by me (using fengyuanchen/datepicker plugin instead):
$('[data-toggle="datepicker"]').datepicker({
autoHide: true,
pick: function (e) {
e.preventDefault();
$(this).val($(this).datepicker('getDate', true));
}
}).keypress(function(event) {
event.preventDefault(); // prevent keyboard writing but allowing value deletion
}).bind('paste',function(e) {
e.preventDefault()
}); //disable paste;
Instead of adding readonly you can also use onkeypress="return false;"
This demo sort of does that by putting the calendar over the text field so you can't type in it. You can also set the input field to read only in the HTML to be sure.
<input type="text" readonly="true" />
HTML
<input class="date-input" type="text" readonly="readonly" />
CSS
.date-input {
background-color: white;
cursor: pointer;
}
I've found that the jQuery Calendar plugin, for me at least, in general just works better for selecting dates.
$('.date').each(function (e) {
if ($(this).attr('disabled') != 'disabled') {
$(this).attr('readOnly', 'true');
$(this).css('cursor', 'pointer');
$(this).css('color', '#5f5f5f');
}
});
Here is your answer which is way to solve.You do not want to use jquery when you restricted the user input in textbox control.
<input type="text" id="my_txtbox" readonly /> <!--HTML5-->
<input type="text" id="my_txtbox" readonly="true"/>
$("#my_txtbox").prop('readonly', true)
worked like a charm..
Instead of using textbox you can use button also. Works best for me, where I don't want users to write date manually.
I know this question is already answered, and most suggested to use readonly attribure.
I just want to share my scenario and answer.
After adding readonly attribute to my HTML Element, I faced issue that I am not able to make this attribute as required dynamically.
Even tried setting as both readonly and required at HTML creation time.
So I will suggest do not use readonly if you want to set it as required also.
Instead use
$("#my_txtbox").datepicker({
// options
});
$("#my_txtbox").keypress(function(event) {
return ( ( event.keyCode || event.which ) === 9 ? true : false );
});
This allow to press tab key only.
You can add more keycodes which you want to bypass.
I below code since I have added both readonly and required it still submits the form.
After removing readonly it works properly.
https://jsfiddle.net/shantaram/hd9o7eor/
$(function() {
$('#id-checkbox').change( function(){
$('#id-input3').prop('required', $(this).is(':checked'));
});
$('#id-input3').datepicker();
$("#id-input3").keypress(function(event) {
return ( ( event.keyCode || event.which ) === 9 ? true : false );
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link href="https://code.jquery.com/ui/jquery-ui-git.css" rel="stylesheet"/>
<form action='https://jsfiddle.net/'>
Name: <input type="text" name='xyz' id='id-input3' readonly='true' required='true'>
<input type='checkbox' id='id-checkbox'> Required <br>
<br>
<input type='submit' value='Submit'>
</form>
replace the ID of time picker:
IDofDatetimePicker to your id.
This will prevent the user from entering any further keyboard inputs but still, the user will be able to access the time popup.
$("#my_txtbox").keypress(function(event) {event.preventDefault();});
Try this
source:
https://github.com/jonthornton/jquery-timepicker/issues/109
This question has a lot of older answers and readonly seems to be the generally accepted solution. I believe the better approach in modern browsers is to use the inputmode="none" in the HTML input tag:
<input type="text" ... inputmode="none" />
or, if you prefer to do it in script:
$(selector).attr('inputmode', 'none');
I haven't tested it extensively, but it is working well on the Android setups I have used it with.
In my case i have use disableTextInput property
$( ".datepicker" ).datepicker({'dateFormat' : 'd-m-y', 'disableTextInput':true });
Or you could, for example, use a hidden field to store the value...
<asp:HiddenField ID="hfDay" runat="server" />
and in the $("#my_txtbox").datepicker({ options:
onSelect: function (dateText, inst) {
$("#<% =hfDay.ClientID %>").val(dateText);
}
If you want the user to select a date from the datepicker but you dont want the user to type inside the textbox then use the following code :
<script type="text/javascript">
$(function () {
$("#datepicker").datepicker({ maxDate: "-1D" }).attr('readonly', 'readonly');
$("#datepicker").readonlyDatepicker(true);
});

Copy Text from Input form into DIV

I want to create 1 input field and one Div.
When u write text in the Input and hit the button i want the text to be displayed in the DIV. Its so simple but it just won't work.. i hope someone can do this easy task for me.
I tried last:
<form>
Bearbeitungstext: <input id="textInput" type="text"><br>
<input type="button" value="text einbinden" onclick="$('texxxt').val($('#textInput').val())">
</form>
<div id="texxxt">
</div>
Try to use id selector properly,
$('#texxxt').text($('#textInput').val());
Also you have to use .text() instead of .val(), .val() is not a function for div elements. It is for form elements which are having value property.
And the best approach for your case would be binding an explicit event handler,
var div = $("#texxxt"),inp = $("#textInput");
$("form button").click(function(e){
e.preventDefault();
div.text(inp.val());
});
This can be done with php $_GET($whatever); also, to do this you will need the name="whatever" on the input field.
Bearbeitungstext:
<input id="textInput" type="text"/><br>
<input type="button" value="text einbinden" onclick="$('#texxxt').html($('#textInput').val())"/>
<div id="texxxt">
to change DIV content we use html() not val()

Focus on first form element inside span when span is clicked

I can't figure out how to focus on the first form element inside a span when the span is clicked. I've included my code, I had also messed around with 'closest' but had no luck.
<form>
<span>
<input id="content" type="text">
<label for="content">Content</label>
</span>
</form>
$('span').click(function() {
$(this).find('input').focus()
});
Any help is appreciated.
Before answering your actual question: there is a way to achieve what you're trying to do which doesn't even require any JavaScript.
If you wrap your input field in the label tag, clicking the label will automatically give focus to the field.
<form>
<label>
Content
<input id="content" name="content" type="text">
</label>
</form>
If you insist on doing it through JavaScript/jQuery, you'll have to make sure you only attach the click handler after the DOM is ready:
$(document).ready(function () { // Or equivalent: $(function() { ... });
$('span').click(function () {
$(this).find('input:first').focus(); // :first makes sure only the first input found is selected
});
});
why not use the label to trigger this functionality
the span will only cover the label and input so if i understand correctly you want the focus to be set on the input even when the user clicks the lable which can be achieved like so:
<form>
<span>
<input name="content" id="content" type="text">
<label for="content">Content</label>
</span>
</form>
But if you are trying to do something else then:
$(document).ready(function(){
$('span').click(function() {
$(this).find('input:first').focus();
});
});
Make sure your js is called after the DOM is loaded
<script type="text/javascript">
$(function(){
$('span').click(function() {
$(this).find('input').focus()
});
});
</script>

Auto-highlight an input field on focus

I was wondering if there was a way for text inside a input box (pre loaded using value="") to highlight when the user clicks on it?
input type='text' name='url' id='url' value='http://www.a-link.com/' />
EDIT
I need the text to he highlighted so the user can copy it.
<input type="text" name="textbox" value="Test" onclick="this.select()" />
You could attach javascript to the click event to select the text like so:
$(document).ready( function() {
$('#id').click( function( event_details ) {
$(this).select();
});
});
There is a potential issue where the user could be trying to click at a later point in the text to correct a typing mistake and end up selecting the whole thing. A better way would be to trigger this when the input gets focus from the user. you'd replace .click with .focus in the example above.
jQuery event documentation:
http://api.jquery.com/category/events/
Add the following onclick attribute to make the entire <input> automatically highlight when the user clicks on it:
<input type="text" value="Test1" onclick="this.select()" />
Alternatively, if you want the user to be able to change the selection after the initial click, change the onclick attribute to an onfocus attribute. This will also highlight the entire <input> when the user clicks on it, but it allows them to change the highlighted part manually afterwards:
<input type="text" value="Test2" onfocus="this.select()" />
Here is an example of both inputs in action.
You want to use focus property. Like this: http://jsfiddle.net/sCuNs/
html
<p><input type="text" size="40"></p>
css
input:focus, textarea:focus{
background-color: green;
}
Do you mean to select the text?
Use onclick event to fire the code:
document.getElementById("target-input-id").select();
$('#foo').on('mouseup', function(e){
e.preventDefault();
$(this).select();
});
$('#foo').on('mouseup', function(e){
e.preventDefault();
$(this).select();
});
This should do it:
<input type='text' name='url' id='url' onclick="this.select()" value='http://www.a-link.com/' />
<input id="inputField" type="text" size="40" value="text to be highlighted"></p>
document.getElementById('inputField').focus();
The default behavior for focus selects the text in the input field. I was looking for a solution not to do that when I found this.

How to select all textareas and textboxes using jQuery?

How can I select all textboxes and textareas, e.g:
<input type='text' />
and
<textarea></textarea>
on a page and have the property style.width="90%"; applied to them?
$('input[type=text], textarea').css({width: '90%'});
That uses standard CSS selectors, jQuery also has a set of pseudo-selector filters for various form elements, for example:
$(':text').css({width: '90%'});
will match all <input type="text"> elements. See Selectors documentation for more info.
Password boxes are also textboxes, so if you need them too:
$("input[type='text'], textarea, input[type='password']").css({width: "90%"});
and while file-input is a bit different, you may want to include them too (eg. for visual consistency):
$("input[type='text'], textarea, input[type='password'], input[type='file']").css({width: "90%"});
$("**:**input[type=text], :input[type='textarea']").css({width: '90%'});
names = [];
$('input[name=text], textarea').each(
function(index){
var input = $(this);
names.push( input.attr('name') );
//input.attr('id');
}
);
it select all textboxes and textarea in your DOM, where $.each function iterates to provide name of ecah element.
Simply use $(":input")
Example disabling all inputs (textarea, input text, etc):
$(":input").prop("disabled", true);
<form>
<textarea>Tetarea</textarea>
<input type="text" value="Text">
<label><input type="checkbox"> Checkbox</label>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

Categories

Resources