I want to run script based on window resize. when I apply if statement not working while browser resize. the current code working only when I reload the page in every screen. I need to do run the script without page load using if and else if media query in jquery. anyone help me to achieve this.
$(window).resize(function() {
if ($(window).width() > 1200) {
$('.menus-area .posts .tileh4 span:first-child').each(function() {
var words = $(this).text();
var maxWords = 22;
if (words.length > maxWords) {
html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
$(this).html(html)
$(this).find('a.read_more_sec').click(function(event) {
$(this).toggleClass("less");
event.preventDefault();
if ($(this).hasClass("less")) {
$(this).html("<i class='fa fa-arrow-left'></i>")
$(this).parent().find(".more_text").show();
} else {
$(this).html("<i class='fa fa-arrow-right'></i>")
$(this).parent().find(".more_text").hide();
}
})
}
});
}
if ($(window).width() < 1199) {
$('.menus-area .posts .tileh4 span:first-child').each(function() {
var words = $(this).text();
var maxWords = 15;
if (words.length > maxWords) {
html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
$(this).html(html)
$(this).find('a.read_more_sec').click(function(event) {
$(this).toggleClass("less");
event.preventDefault();
if ($(this).hasClass("less")) {
$(this).html("<i class='fa fa-arrow-left'></i>")
$(this).parent().find(".more_text").show();
} else {
$(this).html("<i class='fa fa-arrow-right'></i>")
$(this).parent().find(".more_text").hide();
}
})
}
});
}
if ($(window).width() < 991) {
$('.menus-area .posts .tileh4 span:first-child').each(function() {
var words = $(this).text();
var maxWords = 10;
if (words.length > maxWords) {
html = words.slice(0, maxWords) + '<span class="more_text" style="display:none;">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"><i class="fa fa-arrow-right"></i></a>'
$(this).html(html)
$(this).find('a.read_more_sec').click(function(event) {
$(this).toggleClass("less");
event.preventDefault();
if ($(this).hasClass("less")) {
$(this).html("<i class='fa fa-arrow-left'></i>")
$(this).parent().find(".more_text").show();
} else {
$(this).html("<i class='fa fa-arrow-right'></i>")
$(this).parent().find(".more_text").hide();
}
})
}
});
}
if ($(window).width() < 639) {
$('.menus-area .posts .tileh4 span:first-child').each(function() {
var words = $(this).text();
var maxWords = 10;
if (words.length > maxWords) {
html = words.slice(0, maxWords) + '<span class="more_text">' + words.slice(maxWords, words.length) + '</span>' + ' <a href="#" class="read_more_sec"></a>'
$(this).html(html)
$(this).find('a.read_more_sec').click(function(event) {
$(this).toggleClass("less");
event.preventDefault();
if ($(this).hasClass("less")) {
$(this).html("")
$(this).parent().find(".more_text").show();
} else {
$(this).html("")
$(this).parent().find(".more_text").hide();
}
})
}
});
}
});