Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Aleksandrs Karevs
Courses Plus Student 11,178 PointsHow to change the content of a certain HTML paragraph when the viewport is at a certain width using jQuery?
Hi everyone,
I have the following paragraph in my HTML document:
<p class="main-lead">Осуществляем международные автомобильные грузоперевозки</br>между странами Западной,</br>Центральной и Восточной Европы,</br>России и странами СНГ.</p>
How can I change the content of this paragraph (e.g. remove a few </br> elements from it) when the viewport gets to a certain width? As far as I understand it can be achieved using jQuery similar to this:
$( ".main-lead" ).replaceWith( "<p class="main-lead">Осуществляем международные автомобильные грузоперевозки между странами Западной, Центральной и Восточной Европы, России и странами СНГ.</p>" );
What I don't understand, however is how to trigger this jQuery only when the viewport gets to a certain width. It is related to media queries, but I don't know if it is possible to change html content of a certain html element using CSS only.
Any suggestions?
2 Answers
Aleksandrs Karevs
Courses Plus Student 11,178 PointsI think I wrote the script to handle this problem, but it won't work... why?
jQuery(document).resize(function () {
var screen = $(window)
if (screen.width() < 768) {
// if screen width is more than 768px
$(".main-lead").html('Осуществляем международные автомобильные грузоперевозки</br>между странами Западной, Центральной и Восточной Европы, России и</br>странами СНГ.');
}
else if (screen.width() < 992) {
// if screen width is more than 992px
$(".main-lead").html('WORKS! Осуществляем международные автомобильные грузоперевозки</br>между странами Западной, Центральной и Восточной Европы,</br>России и странами СНГ.');
}
});
jared eiseman
29,023 PointsSorry for the slow response, something like this should help get you the desired result though.
$(document).ready(function() {
$(window).on('resize', function() {
if ($(window).width() < 1000) {
$('#target').html('the window is small');
}
else {
$('#target').html('the window is large');
}
});
});