aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Problem with Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problem with Javascript" Watch "Problem with Javascript" New topic
Author

Problem with Javascript

Swela Jathar
Greenhorn

Joined: Aug 19, 2011
Posts: 22
Hi,
I am new to Javascript.I have Jsp file which contains form.I have done validation for date in javascript file.when field is blank i wants to set focus to textfiled.
so how can I access textfield in js file?

Thanks in advance.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Use-


Learning and Learning!-- Java all the way!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

Vic Hood wrote:Use-

That form of referencing elements is not a good practice. It is antiquated and fragile. It should no longer be used.

Rather, assign an id value to the element and reference the element using document.getElementById().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Phelipe Maia
Greenhorn

Joined: Jun 29, 2009
Posts: 14
You should use a JS library.. You probably will need more functions in JS and the libraries will help you to do an easier work. Take a look: http://jquery.com/
Swela Jathar
Greenhorn

Joined: Aug 19, 2011
Posts: 22
Thanks bear,
but it is not working for me.

see my JSP code:
<td colspan="5">
<html:text property="birthDay" onblur="daySize(this);"
maxlength="2"styleClass="datewidth" tabindex="15"/>
<span class="dashEnglish">
<bean:message key="common.slash"/>
</span>
</td>

I hav called daySize function on onblu.

n js code:

function daySize(target){
var length= target.value.length;
var flag=0;
if(target.value=="" || target.value=='' || target.value.charAt(0)==' ' || length==0){
alert("Please enter day value");

// HERE I WANTS TO SET FOCUS TO TEXTFIELD



else
if(length!=0){
intNumber= parseInt(target.value);
if(!isNaN(intNumber)){
if(target.value>=32 || target.value<=0){
alert("Invalid Day");
flag=1;

}
else if(length==1){
target.value="0"+target.value;
}
}
else{
alert("Day Should Be Integer");
flag=1;
}
}
if(flag==1){
// HERE I WANTS TO SET FOCUS TO TEXTFIELD

}

}



Thanks...

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61226
    
  66

Please UseCodeTags and please indicate the specific lines of code that you are having problems with. Be sure that your code is formatted properly.

Also, showing server-side markup isn't helpful. Please show the client-side HTNL.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
target.focus();


Eric
Swela Jathar
Greenhorn

Joined: Aug 19, 2011
Posts: 22
Hi,
by using setTimeout and getElementById problem is solved...

Thanks...
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Why do you have to use getElementById when you are already passing in the object to the function?

Eric
Swela Jathar
Greenhorn

Joined: Aug 19, 2011
Posts: 22
hi Eric,
target.focus() was not working...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Javascript