views:

71

answers:

3

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  A: 

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.

meder
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)
Budda
+4  A: 

For the jQuery piece:

    $('divText').text = 'Changed with jQuery';
    alert(vText.innerHTML);

Should be:

    $('#divText').text('Changed with jQuery');
    alert($('#divText').text());

For the javascript piece:

    var vText = document.getElementById('divText');
    vText.innerText = 'Changed';
    alert(vText.innerHTML);

Should be:

    var vText = document.getElementById('divText');
    vText.innerHTML = 'Changed';
    alert(vText.innerHTML);
Rudu
`$('divText')` should be `$('#divText')`, `vText.innerHtml` should be `vText.innerHTML`.
Marcel Korpel
Thanks for the minor typo corrections. 10 minutes ahead of the answer that copied me, and not accepted *shakes head*
Rudu
Rudu, if a correctly understood you are complaining that another was accepted as an "accepted". Am I right? But actually, you did few mistakes... Your help is really appreciated, and details you provided are clear and useful. That's why answer is voted. But another answer is not worst...
Budda
+2  A: 

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>
Matti