1

I need the function below to return images, it is for a password check and the image should appear as a cross or a tick when its correct or wrong. this is the code so far

<script type="text/javascript">
function checkPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword)
        $("#divCheckPasswordMatch").html("Passwords do not match!" & <img src="img/cross.png" alt="some_text">);
    else
        $("#divCheckPasswordMatch").html("Passwords match." & <img src="img/tick.png" alt="some_text">);
    }

I have tried doing it a couple of ways above si the main way i tried than i also tried to comment the whole image statement but nothing seems to work I'm usually getting syntax errors. i am new to programming as well if this explains why i haven't already figured it out.

1
  • $("#divCheckPasswordMatch").html('Passwords do not match! <img src="img/cross.png" alt="some_text">'); Commented Feb 5, 2014 at 10:29

3 Answers 3

2

The correct syntax is:

$("#divCheckPasswordMatch").html("Passwords do not match! <img src='img/cross.png' alt='some_text'/>");
  • look out for quote mixing - don't break single quotes with double ones and vice versa
  • IMG is an unary tag - it should end with a />
  • .html() method requires a String input - if you join multiple parts use the + operator, not &, so it looks like "abc" + "def"

If you don't want to use two different types of quotes you can escape the inner quotes like this:

$("#divCheckPasswordMatch").html("Passwords do not match! <img src=\"img/cross.png\" alt=\"some_text\">");    

Correct your other HTML setting in a similar manner.

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

1 Comment

+1, although you might also want to mention something about escaping quotes.
1

You need to put the HTML in the string, not just floating about the script on its own after a random Bitwise AND.

"Passwords do not match! <img src=\"img/cross.png\" alt=\"some_text\">"

1 Comment

You might want to mention something about escaping quotes, or use single quotes, seeing as the OP is new to programming.
1

Your syntax isn't correct use either concat operator or as you are not using any js variable you can go with single quote thus you don't need to escape. Use this

<script type="text/javascript">
function checkPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword)
        $("#divCheckPasswordMatch").html('Passwords do not match!<img src="img/cross.png" alt="some_text">');
    else
        $("#divCheckPasswordMatch").html('Passwords match.<img src="img/tick.png" alt="some_text">');
    }

1 Comment

Please provide an explanation for your code. Answers like "use this" decrease the quality of services that SO provides.

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.