1

Hi i'm trying to append elements into one set from there class names but getting an error.

var temp=document.getElementsByClassName("test");
temp.push(document.getElementsByClassName("we"));
2
  • you can't push into temp as it is an object. w3schools.com/jsref/met_document_getelementsbyclassname.asp Commented Dec 29, 2014 at 20:12
  • You've tagged the question with jquery, but aren't doing the jquery way. In jquery, it would be $('.test', '.we') Commented Dec 29, 2014 at 20:27

3 Answers 3

1

That's because getElementsByClassName returns a NodeList, an array-like object which doesn't have push method. You should convert the NodeList into an array and then use the push method.

var temp = [].slice.call(document.getElementsByClassName("test"));

temp.push(...);

Alternatively you can use the querySelectorAll method:

var list = document.querySelectorAll('.test, .we');

And if you are loading jQuery:

var $collection = $('.test, .we');
Sign up to request clarification or add additional context in comments.

2 Comments

is there any or function like and eg. var temp=document.getElementsByClassName("test test1");.actualy i have two classes one "test test1" and other "x x1" how to combine both nodelist into temp. i'm only able to do for the first one
Yes, you can use the querySelectorAll method.
0

You're using jQuery. You can do:

$('.test, .we')

There's also the jQuery .add() method which can be used like:

$('.test').add('.we')

Comments

0

By Using JQuery Yoou can easily get All Elements of Class and then Put These Elements into Another Class.

There is a Code for Solving this Problem :

$(document).ready(function(){
  var a= $(".classname1").html();
  $(".classname2").html(a);
});

This Code is help you to get All Elements of Classname1 and then You can Put them into Classname2.

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.