0

I am trying to replace the content of a <td> element when it exactly matches a string. My code does not seem to work, can anyone please say what I might be doing wrong?

<script>
    $(document).ready(function(){
        $("td").click(function(){
            if (Matches(this, "1")
                ReplaceCellContent(this,"A");
        });
    });

    <!-- Replaces an HTML inner value [find] with a new value [replace] -->
    function ReplaceCellContent(element, replace) {
        $(.element).html(replace);
    }

    <!-- Returns true if [element] contains [stringToMatch] -->
    function Matches(element, stringToMatch) {
        return $(element).text() === stringToMatch;
    }
</script>
6
  • 5
    Hopefully you didn't use HTML comments in javascript? And you have syntax errors, open the console. Commented Mar 21, 2014 at 12:51
  • Yes, I did.. I have changed the comments to javascript style and the console prints Unexpected identifier with line 5, which calls the ReplaceCellContent function. Unfortunately I can't see what is wrong with it. Commented Mar 21, 2014 at 14:29
  • 1
    You're missing a closing parenthesis, and there are more syntax errors. Commented Mar 21, 2014 at 14:32
  • Thanks I had difficulty spotting that, I am used to using Eclipse but have started using Notepad++ for Javascript. Is there perhaps an IDE for JavaScript that will show syntax errors that you can recommend? Commented Mar 21, 2014 at 14:39
  • @Adamme You might wanna look at Sublime for web development. Plugin developed by users are very helpful. Commented Mar 21, 2014 at 14:59

1 Answer 1

1

The problem is that $(element).text() return the text including new line character and tabulation.

Try to trim that value :

return $.trim($(element).text()) === stringToMatch;

You are missing a closing parenthesis :

if (Matches(this, "1"))
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the trim function but it doesn't make a difference. The error is with the ReplaceCellContent function, but I am not sure what why.
Yeah, a missing parenthesis.

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.