1

On the asp page i have created this function to check if two strings are equal:

 <script type="text/javascript">
             function ButtonClick(a, b)
{
  if (a == b) 
  {
     alert("Correct!");
  }
  else
  {
     alert("Wrong!");
  }

}
               </script>

Then, i have created this function which I use when the page loads, to display everything:

public void FillPageSpelling()
    {
        ArrayList videoList1 = new ArrayList();

        if (!IsPostBack)
        {
            videoList1 = ConnectionClass.GetSpelling(1);
        }
        else
        {
            int i = Convert.ToInt32(DropDownList1.SelectedValue);
            videoList1 = ConnectionClass.GetSpelling(i);
        }
        StringBuilder sb = new StringBuilder();
        foreach (Spelling sp in videoList1)
        {

            sb.Append(
           string.Format(
               @"<table class='VideoTable'>

<tr>
                <td align='center'><font face='Verdana'> <font size='3'>Level:</font> <font size='2'>{3}</font></font></td>
            </tr>


            <tr>

                <td align='center'><font face='Verdana'> <font size='3'>Sentence:</font> <font size='2'>{1}</font></font></td>
            </tr>


                <tr>
               <td align='center'><font size='3'>Sound:<audio controls><source src=sound/{2}></audio>
                <font face='Verdana'> <font size='2'> </font> </font></td>

                                 </tr>


<tr>



<tr><td align='center'><font face='Verdana'> <font size='3'>Write the word here: <input type=text name=TextBox1></font></font> </td> </tr>    

<td><button name=btnCheck type=button onclick='ButtonClick(TextBox1.Text, lblWord.Text)'>Check</button>  </td> 
<td><button name=btnCheat type=button onclick='ButtonClick(TextBox1.Text, lblWord.Text)'>Cheat</button>  </td>

</tr>

            <tr>



               <td align='center'><font face='Verdana'> <font size='3'>Word:</font> <font size='2'><asp:Label ID=lblWord runat=server>{4}</asp:Label></font></font></td>


                                 </tr>


</br>


           </table>", sp.SID, sp.Sentence, sp.Sound, sp.Level, sp.Word));
            lblOutput.Text = sb.ToString();



        }

Well, it turns out I have made a mistake here: <td><button name=btnCheck type=button onclick='ButtonClick(TextBox1.Text, lblWord.Text)'>Check</button> </td>

I changed label lblWord to be a textbox - TextBox2 instead, and this is how you should call the function:

<input type=button value='Check' class='p-userButton' onClick='ButtonClick(document.getElementById(""TextBox1"").value, document.getElementById(""TextBox2"").value);'/>
5
  • I think you need another closing brace on the function. Commented Jul 25, 2013 at 22:01
  • @PollyShaw good eye! i fixed it, but it still doesn't work.. Commented Jul 25, 2013 at 22:08
  • What, exactly, is the problem? You haven't supplied anything resembling a problem statement. What does your javascript have to do with the code-behind method? Commented Jul 25, 2013 at 22:15
  • @NicholasCarey, I want to use the javascript function to check if the text written in the TextBox1 is equal to the text of the lblWord Commented Jul 25, 2013 at 22:17
  • @NicholasCarey <button name=btnCheck type=button onclick='ButtonClick(TextBox1.Text, lblWord.Text)'>Check</button> Commented Jul 25, 2013 at 22:18

2 Answers 2

5

JavaScript functions do not have typed parameters. Try your function like this:

function ButtonClick(a, b)
{
  if (a == b) 
  {
     alert("Correct!");
  }
  else
  {
     alert("Wrong!");
  }

}
Sign up to request clarification or add additional context in comments.

3 Comments

To make sure its always a string comparison, maybe a.toString() == b.toString()
+1. Note that === is better when type is expected to match too.
Thank you @Yuriy Galanter, but now every time I click the button it says correct, even if it is not...
0

function ButtonClick(string a, string b)

javascript does not support parameters with data type:

try : function ButtonClick(a, b)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.