Correct my Javascript syntax ?

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);
LVL 8
PeteEngineerAsked:
Who is Participating?
 
ansudhindraCommented:
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

0
 
santhimurthydCommented:
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.
0
 
wdfdo1986Commented:
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
PeteEngineerAuthor 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 !
0
 
santhimurthydCommented:
Try with this

 javascript = "<script type=\"text/jscript\"> $(document).ready(function () { " +
                    "$(\"#datepicker\").datepicker({showOn:\"button\",buttonImage:\"images/calendar.gif\"," +
                    @"buttonImageOnly: true}); });</script>";
0
 
wdfdo1986Commented:
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>";
0
 
santhimurthydCommented:
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
0
 
PeteEngineerAuthor Commented:
santhimurthyd :


Ahhhh..insted of #datepicker i am using client id can u rewrite that for me ?
0
 
santhimurthydCommented:
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>";
0
 
PeteEngineerAuthor Commented:
See this :

"$('" + "#" + this.ClientID + "').datepicker('" +
0
 
ansudhindraCommented:
make it just
"$('#" + this.ClientID + "').datepicker('" +
0
 
santhimurthydCommented:
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.
0
 
PeteEngineerAuthor 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
0
 
PeteEngineerAuthor 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
0
 
PeteEngineerAuthor 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.. :-(
0
 
wdfdo1986Commented:
Post the JavaScript error
0
 
santhimurthydCommented:
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('" +
0
 
PeteEngineerAuthor 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('" +
0
 
santhimurthydCommented:
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.
0
 
PeteEngineerAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.