We help IT Professionals succeed at work.

Correct my Javascript syntax ?

Medium Priority
382 Views
Last Modified: 2012-03-15
Whats wrong in my following syntax ?

string javascript = "<script type='text/jscript'> " +
"$(document).ready(function () { " +
"$('" + "#" + this.ClientID + "').datepicker('" +
"'{ showOn: '" + "button" +"',buttonImage: '"
+ "images/calendar.gif,buttonImageOnly: true}); ); " 
+ "});</script>";

Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, javascript);
Comment
Watch Question

try this..

string javascript = "<script type='text/jscript'> " + 
"$(document).ready(function () { " + 
"$('#" + this.ClientID + "').datepicker(" + 
"{ showOn: 'button',buttonImage: " +
"'images/calendar.gif',buttonImageOnly: true}); ); "+
"});</script>"; 

Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, javascript);

Open in new window

CERTIFIED EXPERT
Commented:
Tried to fix the issue and felt as it's difficult until we don't as what's your final syntax requirment but follow the steps to correct the syntax

Instead of registering the script usign the
Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, javascript);


Declare the string varaible at class level instead of Method level as show below

protected string javascript = string.Empty;

Open in new window


In the .aspx page place the belwo syntaxt to get the Script generated
<%=javascript%>

Open in new window


finally following the step script generated is as follows

 
<script type='text/jscript'> 
            $(document).ready(function () 
            { 
                $('#__Page').datepicker([b]''{ showOn: 'button',buttonImage: 'images/calendar.gif,buttonImageOnly: true}); ); });[/b]            
        </script>

Open in new window


bolded the place where the issue is, If you can possible as what's your expected script will help to fix the syntax error.
yeah as ansudhindra corrected you have included some extra quotations there. Best way to test the code is first use it in an test html file and check it.

Author

Commented:
This  is my expected :

<script>
      $(function() {
            $( "#datepicker" ).datepicker({
                  showOn: "button",
                  buttonImage: "images/calendar.gif",
                  buttonImageOnly: true
            });
      });
      </script>


But the one "ansudhindra"  posted is not working :-(

Please correct and post !
CERTIFIED EXPERT

Commented:
Try with this

 javascript = "<script type=\"text/jscript\"> $(document).ready(function () { " +
                    "$(\"#datepicker\").datepicker({showOn:\"button\",buttonImage:\"images/calendar.gif\"," +
                    @"buttonImageOnly: true}); });</script>";
santhimurthyd's concept is right but as I know to access a variable which is created n the back end code from the front end you need to make it a property. However that's the way I am doing when I wanted to do what you tries to do.

And if you want to continue this try this
string javascript = "<script> $(function() { $(\"" + "#" + this.ClientID + "\").datepicker(\"{ showOn: \"button\", buttonImage: \"images/calendar.gif\" ,buttonImageOnly: true}); }); </script>";
CERTIFIED EXPERT

Commented:
I'm trying with the Page.register script only and not with the Varaible access

  javascript = "<script type=\"text/jscript\"> $(document).ready(function () { " +
                    "$(\"#datepicker\").datepicker({showOn:\"button\",buttonImage:\"images/calendar.gif\"," +
                    @"buttonImageOnly: true}); });</script>";

            Page.ClientScript.RegisterStartupScript(typeof(string), Guid.NewGuid().ToString(), javascript);


This is the syntax, I did and got error due to non reference to JQuerry Library.

If possible share the error else "Uncheck" the option disbale Javascript debuggin in IE to find where is the error your getting will help us

i) Open IE
ii) Navigate to "Tools" => "Internet Options"
iii) Navigate to "Advance Tab"
iv) Uncheck checkbox "disbale Script Debugging" under the Browser category

share your steps to help further

Author

Commented:
santhimurthyd :


Ahhhh..insted of #datepicker i am using client id can u rewrite that for me ?
CERTIFIED EXPERT

Commented:
Cool , In the below change the bolded text with the respective DataPicker Id

javascript = "<script type=\"text/jscript\"> $(document).ready(function () { " +
                    "$(\"#" + <<datepicker>>.ClientID +"\").datepicker({showOn:\"button\",buttonImage:\"images/calendar.gif\"," +
                    @"buttonImageOnly: true}); });</script>";

Author

Commented:
See this :

"$('" + "#" + this.ClientID + "').datepicker('" +
make it just
"$('#" + this.ClientID + "').datepicker('" +
CERTIFIED EXPERT

Commented:
this.ClientID  will return you the page Id as "__Page" to get the Datapicker Control Id, you have to use Datapicker Control Id instead of this.

Author

Commented:
no ..its working for the code that i posed above in my question ... u just need to change the syntax ... no change in code contents required ..please put the this.clientid and rewrite the code you have posted

Author

Commented:
Almost worked but few more issue


  string javascript = "<script type=\"text/jscript\"> $(document).ready(function () { " +
                    "$(\"#" + this.ClientID +"\").datepicker({showOn:\"button\",buttonImage:\"calendar.gif\"," +
                    @"buttonImageOnly: true}); });</script>";


The above code i have rewritten to read the calender.gif which is not in image folder.
The earlier one was not in image folder. Not its in root folder.

But i am getting an javascript error ..please fix it for me too

Author

Commented:
The previous code is working which take image from image folder

the above one is not as i am taking it NOT from image but from root.. :-(
Post the JavaScript error
CERTIFIED EXPERT

Commented:
What's the control Id needs to be in the "$(\"#" + this.ClientID +"\").datepicker because that's the decision one. If you cna explain more will help full

because in your script you have mentioned it have to be as
 $( "#datepicker" ).datepicker({

Where as in code behind implementation client Id of the page is used
"$('" + "#" + this.ClientID + "').datepicker('" +

Author

Commented:
I cannot post the error since it is visible

But i need to get the above posted javascript without images folder


i dont want to be like this : ( "#datepicker" ).datepicker({

instead i want to be like this :  $('" + "#" + this.ClientID + "').datepicker('" +
CERTIFIED EXPERT

Commented:
ok, But could you give more inputs because this.ClientID will always retun the page id
"__Page" , not the "datePicker". so if you can use the control id instead of "this" will create the client id of the control to which the datepicker to be show.

If i'm correct you may mapped the event to button, In that case use the Button Id not this.

Author

Commented:
Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.