14

I started reading JavaScript in W3schools and testing out/changing few things in the examples they give so I can see what is doing what but didn't manage to identify the syntax, yet.

Below is the original code to change p tag content, the link to it.

<p id="demo">
    JavaScript can change the content of an HTML element.
</p>

<script>
function myFunction()
{
    x = document.getElementById("demo");  // Find the element
    x.innerHTML = "Hello JavaScript!";    // Change the content
}
</script>

<button type="button" onclick="myFunction()">Click Me!</button>

I want to know how to change contents with the same class, but failed as you can see that the example below doesn't work. Fiddle of code below.

<p class="demo">
    JavaScript can change the content of an HTML element.
</p>

<p class="demo">Yolo</p>

<script>
function myFunction()
{
    x = document.getElementsByClassName("demo");  // Find the element
    x.innerHTML = "Hello JavaScript!";    // Change the content
}
</script>

<button type="button" onclick="myFunction()">Click Me!</button>

If you could show me how ^^" and help me understand, is "getElementById" a variable that could be anything else or is it a command?

3 Answers 3

25

Your x - is array of elements. try to use loop:

<body>

<p class="demo">JavaScript can change the content of an HTML element.</p>    
<p class="demo">Yolo</p>   

<button type="button" onclick="myFunction()">Click Me!</button>

<script>        
function myFunction()
{
x=document.getElementsByClassName("demo");  // Find the elements
    for(var i = 0; i < x.length; i++){
    x[i].innerText="Hello JavaScript!";    // Change the content
    }

}

</script>
</body>

See FIDDLE

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

5 Comments

In the fiddle you have an extra line "console.log(x)" D:: what is it?
it just displays a message in the console.
I use it just to make sure that everything I do is right
Oh ok, I thought it was strage since it worked normally without it. Thanks!
4

Notice how when you use:

x=document.getElementsByClassName("demo"); 

It is Elements instead of Element. This is because it returns an array a HTMLCollection of all the elements with one particular class name. In order to combat this, you can choose the first element in the array:

x=document.getElementsByClassName("demo")[0];

3 Comments

I read your answer like 4 times then I understood the array thingy! (I had an idea of array from Pascal). Tested it and worked, [0] changed the first text, [1] changed the 2nd. Thanks! Tho the loop thingy was what I intended
var x=document.getElementsByClassName("demo")[0].innerHTML; but i want to access this using jquery then how can i get variable x
You will get variable x using jQuery like this x = $('.demo');
3

It is easier to use jQuery with Javascript

See this example: http://jsfiddle.net/37jq9/3/

If you use jquery instead of calling

x=document.getElementsByClassName("demo");

you can use

x = $('.demo');

but you can just call the function like this:

$(document).ready(function(){
    $('button').click(function(){
        $('.demo').text('Hello Javascript');
    })   
})

2 Comments

"I started reading JavaScript in W3schools" - Harlequin should learn javascript at first.
"Easier to use jQuery with Java"? — what? Java != JavaScript, and ideally JavaScript should be learned before developing a reliance any library.

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.