0

Here is source code of the simple page:

<html>
<head>
    <title>Test</title>
    <script src="jquery-1.4.2.js" type=text/javascript></script>
</head>
<body>
    <div id="divText">Original</div>

    <script type="text/javascript">
        var vText = document.getElementById('divText');
        vText.innerText = 'Changed';
        alert(vText.innerHTML);
        $('divText').text = 'Changed with jQuery';
        alert(vText.innerHTML);
    </script>
</body>
</html>

"jquery-1.4.2.js" file is in the same folder.

Both alerts display "original" text, text in browser also "Original"...

What is wrong with my code? Any thoughts are welcome.

2 Answers 2

3

1. A quick google ( took me 2 seconds ) reveals text is a function, not a property. Use it like .text('lol') as the example directly from the API.

http://api.jquery.com/text/

2. innerText isn't available in every browser/DOM property.

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

1 Comment

1. Agree, also saw that but 'lost'... 2. Yup, the code is on my work and it didn't work in Chrome, but worked in FF (or vice versa)
2

As you pointed out in the title you'll be wanting to change the inner html. You'll also need the $('#divText') selector to get to the div with jQuery.

<html>
<head>
    <title>Test</title>
    <script src="jquery-1.4.2.js" type=text/javascript></script>
</head>
<body>
    <div id="divText">Original</div>

    <script type="text/javascript">
        var vText = document.getElementById('divText');
        vText.innerHTML = 'Changed';
        alert(vText.innerHTML);
        alert($('#divText'));
        $('#divText').html('Changed with jQuery');
        alert(vText.innerHTML);
    </script>
</body>
</html>

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.