Solved

Javascript onFocus Blur

Posted on 2016-08-31
4
56 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 22

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now