Solved

Javascript onFocus Blur

Posted on 2016-08-31
4
73 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 54

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
More SharePoint App with Angular and Bootstrap 17 48
https://github.com/willowsystems/jSignature 11 50
PHP Form Calculate Total Price 10 42
Index on a Table 6 12
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

770 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