0

I've got this below in jQuery, Where on a window width less than 1024px, I want to append ".moveThisContent" and move it to ".newLocation" what the best way to do this in Javascript is? Would it be to use .appendChild

if($(window).width() < 1024){
  $(".newLocation").append($(".moveThisContent"));
}
13
  • This is JavaScript? I'm not sure what you're trying to ask. Does this not work? Commented Mar 31, 2021 at 10:19
  • 2
    Yes, .appendChild() is one possible option. Commented Mar 31, 2021 at 10:20
  • 1
    Note that asking for "best way" is off topic as it's opinion based. You're probably meaning to ask "how can I do this" Commented Mar 31, 2021 at 10:29
  • 1
    @traktor sorry I do understand what jQuery is. It's that the OP doesn't think it's JavaScript Commented Mar 31, 2021 at 10:29
  • 1
    @jqueryHtmlCSS youmightnotneedjquery.com/#append Commented Mar 31, 2021 at 10:32

4 Answers 4

1

You can use getElementsByClassName and for set and get content you can use innerHTML

const width  = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
if( width < 1024 ){
    document.getElementsByClassName('newLocation')[0].innerHTML = document.getElementsByClassName('moveThisContent')[0].innerHTML;
    document.getElementsByClassName('moveThisContent')[0].innerHTML = '';
}
.moveThisContent{color:blue;}
.newLocation{color:red;}
<div class="moveThisContent"> lorem ipsum lorem ipsum </div>
<div class="newLocation"></div>

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

Comments

1

With this i've used .appendChild and that does what i need.

Comments

0

Expanding a bit on this answer https://stackoverflow.com/a/66886186/3825777, I thought I'd try and make things visually clear for visual learners. I also wanted to make it clear what width we are talking about. Please look at the console to see if the width you think you are talking about is less than or greater than 1024.

$('#sWidth').text(window.screen.width);
$('#sHeight').text(window.screen.height);
$('#wWidth').text($(window).width());
$('#wHeight').text($(window).height());


const width  = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
console.log(width);
if( width < 1024 ){
    document.getElementsByClassName('newLocation')[0].innerHTML = document.getElementsByClassName('moveThisContent')[0].innerHTML;
    document.getElementsByClassName('moveThisContent')[0].innerHTML = '';
}
.moveThisContent{color:#111111;}
.newLocation{color:#777777;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Screen width: <span id="sWidth"></span><br>
Screen height: <span id="sHeight"></span><br>
Window width: <span id="wWidth"></span><br>
Window height: <span id="wHeight"></span><br>



<div class="moveThisContent"> This inner html content is inside a div with class name moveThisContent and in the css this has the color #111111.  I chose the number one not because I like the color 111111, but because I wanted to make it known that his is the first div in the HTML. The content of this div which is the text that you are reading should only move to the div with class name newLocation if the width of your screen is less than 1024 pixels.  The color of this other div is #777777. You will know the content moved if the text you are reading is grey.</div>
<div class="newLocation">The class name of this div is newLocation and the CSS hexidecimal color code for this div is 777777.  The content of this div will change to the content of the div before it if your screen width is less than 1024 pixels  </div>

Comments

0

Here is the way of what you want to do

You can refer to this link Here

You can use media query in javascript for checking window width

if (window.matchMedia("(max-width: 1024px)").matches) {
  /* The viewport is less than, or equal to, 1024 pixels wide */
  var element = document.getElementByClassName("newLocation");
  element.classList.add("moveThisContent");
} else {
  /* The viewport is greater than 1024 pixels wide */
}

4 Comments

I don't think the OP wants to add a class name. Just move the content from one element to another.
moveThisContent already exists in the DOM.
// For Moving the content you can use following way from #myDiv1 to #myDiv2 $('#myDiv2').append( $('#myDiv1>p') );
@neilnikkunilesh yes, that's what they're already doing - they want to move content without jquery.

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.