Solved

Javascript onFocus Blur

Posted on 2016-08-31
4
47 Views
Last Modified: 2016-09-01
I want to have a normal HTML form with various input fields but in two of the fields, depending upon conditions, I don't want to allow the user to update an existing value.

As I recall, if I do the following in the field definition, it accomplishes that.

<input type = test" name="fname" value ="ABC" onFocus="blur();">

Is this correct?

Thanks
0
Comment
Question by:Richard Korts
4 Comments
 
LVL 6

Expert Comment

by:Mikkel Sandberg
ID: 41779007
Hey Richard,
You can also use the "disabled" attribute in the <input> element. For example:
if (someCondition) {
  document.getElementsByTagName("input").disabled = true;
}

Open in new window

Or if you prefer jQuery:
if (someCondition) {
  $('input').attr('disabled', true);
}

Open in new window

0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 41779203
If you decide to use jQuery, you should use the prop() method instead of attr() according to the jQuery documentation.
The .prop() method should be used to set disabled and checked instead of the .attr() method.
if (someCondition) {
  $('input').prop('disabled', true);
}

Open in new window

0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41779385
Why not just use the readonly attribute

<input type = test" name="fname" value ="ABC" readonly />

Open in new window

0
 
LVL 11

Expert Comment

by:Murfur
ID: 41779527
I wouldn't recommend using the disabled property as a disabled element will not be submitted with the form.
 
I think the easiest route is Julian's suggested use of the readonly property although you can also make use of what Mikkel and Kim suggested and use jQuery to remove the readonly property according to the the conditions you mentioned in your original post i.e. start the form with the field being readonly and then remove the property once the qualifying conditions have been met.
if (someCondition) {
    $('input').prop('readonly', false);
}

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now