Solved

Javascript onFocus Blur

Posted on 2016-08-31
4
105 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 57

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

710 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