This is a migrated thread and some comments may be shown as answers.

Calling a javascript method when a button is clicked

3 Answers 178 Views
Button
This is a migrated thread and some comments may be shown as answers.
Deepika
Top achievements
Rank 1
Deepika asked on 12 Dec 2011, 04:31 AM
Hello,

I have 2 dropdowns, 2 textboxes and two buttons in my form. OnClick the button does certain things. before that, I need to validate a condition. the condition is that if only the txtbox2 is clicked, and no other parameter is selected / entered, then I should show a pop up saying the "txtBox 2 alone can't be entered, and Please also select item from first dropdown!".. Logic needed is not complicated. I am having a problem in finding the object in my javascript function.

I ahve called IsTREmpty() on OnClientClick event for the button. I am getting the error saying object required. How can I access the form element in my javascript.?

function

IsTREmpty() {  

var tr = document.getElementById('rdCmbTR').value;  

var tt = document.getElementById('rdCmbTT').value;  

var tid = document.getElementById('rdTxtTId').value;  

var tDesc = document.getElementById('rdTxtTDesc').value; 
alert(tDesc.length); 

if (tDesc.length > 0) {  

if (tt == -1 && tid.length == 0 && tr == -1) {  

alert('Please Select T R'); }

}

}

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Dec 2011, 06:17 AM
Hello,

You can access RadControls by using the following method.
JS:
var menu = $find("<%=rdCmbTR.ClientID %>");

Thanks,
Princy.
0
Deepika
Top achievements
Rank 1
answered on 12 Dec 2011, 06:55 AM
Hey Princy,

I am getting some error. I think I shud nt use .value. But I need the value to compare. Ny solution?
0
Slav
Telerik team
answered on 13 Dec 2011, 04:43 PM
Hi Deepika,

There are two methods, supplied by the Microsoft AJAX Library, that you can use for referencing HTML elements on your page:
  • $find - provides a shortcut to the Sys.Application.findComponent() method, which returns the specified
    Component object. Expect to use this method every time you reference a RadControl on the client.
  • $get - this method is just for finding generic HTML elements, not RadControls. It provides a shortcut to the getElementById() method.

As most probably you are using the RadButton control, even if not explicitly mentioned, please note that it doesn't support an OnClientClick client event. Instead there are two events, which provide similar functionality - OnClientClicking and OnClientClicked. In your case OnClientClicking should be used, since the click event of the RadButton needs to be canceled if a certain condition is not met.

Please find attached a sample project, implementing a possible solution for the described case. You can use it as a reference for your further development.

Regards,
Slav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Button
Asked by
Deepika
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Deepika
Top achievements
Rank 1
Slav
Telerik team
Share this question
or