473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

id of element when lost focus

I need to know how can obtain the id of a element when this lost focus, the reason is because I have more than one editbox and my function needs of the id for validation, could you help me with a little idea about this problem

Thanks
Aug 20 '08 #1
13 3047
RamananKalirajan
608 512MB
Hi I hope this code will help u out. Any problem post it back I will try to help u out

[HTML]<html>
<input type="text" id="myText" onblur="doThis(this)">
<input type="text" id="myText1" onblur="doThis(this)">

</html>
<script type="text/javascript">
function doThis(ths)
{
alert(ths.id);
}
</script>[/HTML]

Regards
Ramanan Kalirajan
Aug 21 '08 #2
gits
5,390 Expert Mod 4TB
did you notice that your script section is outside the html-section? this will basicly invalidate the page - even when it seems to work ...

kind regards
Aug 21 '08 #3
RamananKalirajan
608 512MB
did you notice that your script section is outside the html-section? this will basicly invalidate the page - even when it seems to work ...

kind regards
Sorry Mr. Gits I am currently involved in Prototype JS framework. i will correct my mistakes. it will not happen again

Regards
Ramanan Kalirajan
Aug 21 '08 #4
rnd me
427 Expert 256MB
i in a project i did, i decalred a variable called lastText.

then i used onblur="lastText=this;" on each textbox.

then i could just access lastText from any function, and the last used textbox is referenced.
Aug 21 '08 #5
gits
5,390 Expert Mod 4TB
Sorry Mr. Gits I am currently involved in Prototype JS framework. i will correct my mistakes. it will not happen again

Regards
Ramanan Kalirajan
no need to apologize for ;) ... it was just a hint for those who will read that thread and may be wondering about this ...

kind regards
Aug 21 '08 #6
Thanks a lot, i was applying the expression in a different way, but after i Thought better your solution, thank for your help
Aug 21 '08 #7
RamananKalirajan
608 512MB
i in a project i did, i decalred a variable called lastText.

then i used onblur="lastText=this;" on each textbox.

then i could just access lastText from any function, and the last used textbox is referenced.

Hi I am interested in ur concept. But some silly doubts. You declared the variable lastText globally and used the value as u specified above. It was amazing. I will try it out in my project also. Thanks for the idea

Regards
Ramanan Kalirajan
Aug 22 '08 #8
chaarmann
785 Expert 512MB
You declared the variable lastText globally and used the value as u specified above. It was amazing. I will try it out in my project also. Thanks for the idea

Regards
Ramanan Kalirajan
Don't do it, it's a very, very bad idea!
Most companies have a coding standard that says: don't use global variables!!!
Just imagine all the things that can go wrong and how unreadable the code becomes.
For example:
a) somebody has the same idea and also declares a global variable lastText that he needs for his function that should show the last error message. He works on a different part of javascript (or js-file) than you, but on your current page, his and your code are merged. So lastText is a string now (the error message) and not an object anymore! All your functions that were using "lastText" start crashing. And even if you verify right before, it still might crash!
example:
your code:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. <input type="text" id="myText1" onblur="lastText=this">
  3.  ...
  4. var lastText;
  5. function doThis()
  6. {
  7.   if (typeof(lastText) == "string") return; // test if it is an object or an error message
  8.    alert(lastText.id);
  9. }
his code:
Expand|Select|Wrap|Line Numbers
  1. setInterval("errorTest", 1000);
  2. function errorTest()
  3. {
  4.  var errorHappened = ... // test if an error happened or not
  5.  if (errorHappened) lastText="An error happened!";
  6. }
Why is it crashing???

the thread errorTest is executed every second (see setInterval()) as a background thread.

Now somebody click on the textbox. if you are lucky, everything goes well. But It can also crash:

Let's say the foreground thread that executes your onblur() just stopped after executing line 7 of your code (right before the alert() message. It was interrupted by the background thread that tests for errors. This background thread now executes line 5 in his code and sets lastText to an error string. Now it is finished and your foreground thread that was interruped continues. It executes line 8 of your code and crashes, because lastText is now changed from an object to a string, therefore lastText.id does not exist!
These types of errors are very hard to debug:
your code alone works all well, his code alone works all well, but if both comes together on one page, it crashes sometimes. Because it's not always but only sometimes, you can't debug the code in a debugger. This error is very, very hard to catch! And all because you and he did not listen to the golden rule "Don't use global variables"!!! It all gets worse when you and him are from different companies! Whose fault is it, who should spend money to fix the code and retest it? A manager nightmare also.

b) readability:
You could write thousands of lines of codes in different js-files and includes HTML-pages (framesets) that make up your current webpage.
Now suddenly you see a function:
function showErrorMessage()
{
alert(lastText);
}

So where is lastText defined? What initial value does it have? How can I make sure that it is always defined when somebody enters the function?. In which of the different HTML and js files is it used? Ok, you can search for it with an editor, but it will also show you all the source code like "onBlur=lastText=this" and so on which have nothing to do with the error.
Your task of rewriting/extending the error messages becomes a nightmare, because you must understand the whole source code to know which line you should leave (because it deals with textboxes) and which line to change (because it deals with your error message). Can you understand thousands of lines of sourcecode withing 10 minutes, most of them you have not written yourself???. no? Me neither! I would need days to do it.
So stick to the golden rule "no global variable" and you (or somebody else) could have done the needed changes withing 10 minutes.
Aug 22 '08 #9
RamananKalirajan
608 512MB
See I am not currently using in it on any project but that idea seems to be matching requirement when i tried to validate the date field. Thats why. Can u please tell me the other way how can I do without using the global variable.

Regards
Ramanan Kalirajan
Aug 22 '08 #10
gits
5,390 Expert Mod 4TB
@chaarmann:

basically i agree with you that globals are evil - you may end up in the 'globals hell' when you use it without control ... but in larger projects ... that (i assume that) you are talking about, this is controlled within a 'framework' ... or at least it should be! :) ... so the event-handling for every page should be centralized so that every programmer HAS TO use this and having said that within this centralized handling it could be ensured through convention and using objects that encapsulate data - that this data could be used globally and in a reliable way ...

kind regards
Aug 22 '08 #11
chaarmann
785 Expert 512MB
Can u please tell me the other way how can I do without using the global variable.
You already gave the solution in your first forum entry. (#2 05:54 AM). The only thing you need to change is to put all the javascript inside the html tag, not outside, then it should work (as mentioned before).
Aug 22 '08 #12
chaarmann
785 Expert 512MB
@chaarmann:

basically i agree with you that globals are evil - you may end up in the 'globals hell' when you use it without control ... but in larger projects ... that (i assume that) you are talking about, this is controlled within a 'framework' ... or at least it should be! :) ... so the event-handling for every page should be centralized so that every programmer HAS TO use this and having said that within this centralized handling it could be ensured through convention and using objects that encapsulate data - that this data could be used globally and in a reliable way ...

kind regards
You are right. You can also do it with centralized event handling and global variables that every programmer has to use insetad of the golden no-global-variables rule. But how can you communicate these conventions to external programmers? Or source code that you bought from another company? These programmers don't have the time to read your framework or meaning of global variables. They get a fixed price to deliver the pages and want to do it fast. How can you communicate to the computer-illiterate management that the foreign code works, but must still be changed before they pay for it, because after paying nothing will be done anymore?

I was technical leader in a company with over 20 thousand employees responsible for the internal employee portal. We had no such framework or coding standards because of historical reasons. It was very chaotic: The initial code was bought, then alot of code was programmed by our team, and much was added on later by subcontractors and external programmers. Most of these programmers (except our team) were newbies with copy-and-paste and try-and-error mentality: search for a code snippet in internet, copy it, change some variables around with try-and error until it works 90%. Deliver fast and get paid. Then refuse to clean-up or fix problems related when running with other code together ("But it works on my system, so it's not my bug! The other company should fix its code!"). We had a lot of cases where 2 programmers were copying from the same code snipped in internet, so the global variables were exactly overwriting each other! They had the mentality of no need to look for already-existing code and re-use it (it's much faster to get a fresh copy from internet than trying to understand the first code and what changes the first programmer had done, then modularize it as a general object or function and then make it usable also for my own page.) I mean it was faster to develop in the beginning this way, but seen in a long developing cycle with testing etc. it was much, much more work and slower at all.
We ended up with the rule "No global variable". It was a huge task that our group did to rewrite all the existing code, but it was successful. Sometimes it was easier to throw away the whole code that we just bought from an external programmer and rewrite it ourself. We deleted thousands of global variables and only left around 3 where it really was not possible to have them local. (among them for example a global variable DEBUG)
And in the end when we finished it paid off: quick changes were possible and we had a much faster and stable website.

Example for avoiding the global variable "menu" (only if you cannot avoid it in another, better way):
We had <div id=menu>
Then we accessed with var menu=document.getElementById("menu");
Then you had to check in your code (a must!) if menu was null (not defined) or an array (two divs with the same name defined). If so, then we displayed an error. So we detected errors easily and could change code accordingly.
Aug 22 '08 #13
gits
5,390 Expert Mod 4TB
we had the same problems you describe ... but fortunatly we have our own custom framework and every external programmer HAS TO use it ... and HAS TO follow the guidelines, and HAS TO be aware of our conventions. what if not? who shall maintain such code that doesn't follow the rules ... always the externals? no! ... so we ensure that the programmer commits to the rules and i review the code ... when there is something broken i just regret it ... when the programmer don't want to follow the rules he won't get the order and when he commits and then break the rules he won't get paid unless it is fixed. we have over 400000 loc of javascript code and you cannot handle that without such a strict policy.

kind regards

PS: of course --> globals are evil and are to avoid whenever possible - that should always be the rule!!!! ... i just wanted to point out that you may control the use of it (and should) since you cannot always avoid them.
Aug 22 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Paul Thompson | last post by:
How do I determine in JavaScript the name of the object that focus is on?
11
by: Dom | last post by:
Hi, I wanted to use the Button Element to submit a form like this example: <form action="/test.exe" method="post"> <button id="ID_BUTTON_OK" name="_WEB_EVENT_HANDLER_" type="submit"...
3
by: Praveen | last post by:
In IE a table element will receive focus when you either tab into it or when you click anywhere within the table. Mainly it fires the onfocus event. This doesn't happen in Mozilla (Firefox and...
3
by: Tom | last post by:
I have a VB .NET application that has a text box with the following code to handle the leave event. Private Sub txtIDiscountRate_TextChanged(ByVal sender As System.Object, ByVal e As...
3
by: Burak Kadirbeyoglu | last post by:
Dear Developers, I have a simple question. There's a textbox and an imagebutton on my page. When I focus on the textbox and hit enter, the imagebutton is being clicked. However, when I click...
2
by: yawnmoth | last post by:
Say I have two input elements and that I wanted to make it so that when the first ones input was what it should be, the focus would automatically be shifted to the next input element. ie....
3
by: viral123 | last post by:
Hi all, does any one know how to get the functionality of get focus and lost focus using ASP.Net like VB.Net I want to change the textbox back ground color when it has the focus. I used...
1
by: ravikumar2007 | last post by:
I am working with .NET 3.5 framework within an WPF application. I have a windows application where for a textbox, I have defined the lost focus event. I need to call the Focus() method of any other...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.