How to load different HTML based on window.width()? - javascript

How to always listen to window.width() and load different HTML codes based on the width values? I'm using window.width() and replaceWith().
It's only works when I open the page and not working when I resizing my browser window.
My code inside my html file:
var width;
$(window).resize(function() {
width = $(window).width();
if (width < 768) {
$("#the_dropdown").replaceWith("<p>Less than 768</p>");
} else {
$("#the_dropdown").replaceWith("<p> More than 768</p>");
}
});
$(window).trigger('resize');
Demo
Another question, does replaceWith() suitable for situation that involves long HTML codes (more than 1 line)?
I've already done some sets of HTML codes based on specific window's width and I want to put it accordingly inside my #the_dropdown div.

Your code doesn't work because replaceWith() replaces the selected element. In your case, the element with id #the_dropdown. Therefore on the next trigger, this element isn't found and no text is written.
Replace replaceWith() with .html().
Demo

Related

How to get inner html based on inner div height?

As per my requirement i'm looking for jquery code which could do pagination by html contents..
I did some research on it but didn't get any feasible solution for the same.
here is sample code what I'm trying to achieve:
<div id="parent">
<div id="child" style="height:30%">---contents-----</div>
</div>
now according to requirement I'm using below js code to detect if div is overflown.
checkOverflow () {
let element = document.getElementById('child');
if (element.offsetHeight < element.scrollHeight ||
element.offsetWidth < element.scrollWidth) {
return true;
} else {
return false;
}
}
above function is returning true if contents are overflown on child div, now I want to truncate overflown contents to second page and add pagination on page so user can click on button to display more contents on second page.
I don't know how to achieve above thing but I've tried to get contents position of element.offsetHeight, but that didn't works
Thanks.

Switch HTML content of div with another piece of HTML without fading out to white first using jQuery

I'm looking for a way to fade from one piece of HTML in a div to another piece of HTML without fading out to white (to the background color of the div) first like i'm doing in this example:
The positionNumber variable is just an int with the number (e.g. 3).
function changeContent(positionNumber) {
$('.banner-content-wrapper').fadeOut('fast', function() {
var contentHtml = $('.slidercontent#' + positionNumber).html();
$('.banner-content-wrapper').hide().fadeIn(1000).html(contentHtml);
});
}
This example does the exact same by fading to white first but this is not what I'm lookig for:
Why doesn't jquery fadeIn() work with .html()?
I want to fade directly from one piece of HTML to another. I haven't been able to find any example on Stack Overflow that shows how to do exact that. I know this is not valid code but it's something like this I'm looking for:
$('.banner-content-wrapper').fadeToHtml(contentHtml);
How can I fade directly?
why use fadeOut/fadeIn - why not hide/show
function changeContent(positionNumber) {
$('.banner-content-wrapper').hide('fast', function() {
var contentHtml = $('.slidercontent#' + positionNumber).html();
$('.banner-content-wrapper').hide().html(contentHtml).show();
});
}
I have not tested this - just a suggestion based on the example you referenced
I don't think you should use fade. Try to use Replace.
http://api.jquery.com/replacewith/

Displaying and highlighting a particular line in HTML

I am trying to implement a webpage which should have expected to have the following properties.
The HTML page contains many lines of text (thousands of lines), basically a log file.
Upon a desired action, line which is related to the action should be highlighted and shown . (exactly the way that would happen if you click on corresponding source button of a logged variable in chrome inspect element.)
This seems to be very basic but I couldn't figure out how! May be I am missing some literary terms.
Thank you.
You need to do a few things:
$("li").each(function(i, element) {
var li = $(element);
if (li.text() == "Orange") {
li.addClass("selected");
// Get position of selected element relative to top of document
var position = li.offset().top;
// Get the height of the window
var windowHeight = $(window).height();
// Scroll to and center the selected element in the viewport
$("body").scrollTop(position - (windowHeight/2));
}
});
See DEMO.
There are many ways to go about this. But is there any class tags in the logged source or is just one large text block?
If there are class or id tags on the html you can use javascript or jquery to do this.
document.getElementById('myText');
or in jquery
var element = $("#myText");
//example css changes
element.css("position","center");
element.css("color","red");
Then change the css style on those html elements.

Javascript show/hide - I don't want it to hide the entire element

This is probably a fairly easy question, but I'm new to JavaScript and jquery....
I have a website with a basic show/hide toggle. The show/hide function I'm using is here:
http://andylangton.co.uk/articles/javascript/jquery-show-hide-multiple-elements/
So here's my question..... I would really like the first 5-10 words of the toggled section to always be visible. Is there some way I can change it so that it doesn't hide the entire element, but hides all but the first few words of the element?
Here's a screenshot of what I would like it to do:
http://answers.alchemycs.com/mobile/images/capture.jpg
There are many different implementation possibilities:
You can divide the contents up into the first part and the second part (two separate spans or divs inside your main object) and hide only the child object that represents the second part, not hide the parent object.
Rather than hide the object at all, you can set its height to only show the first part (with overflow: hidden)
Change the contents of the main object to only have the first part as the contents (requires you to maintain the full contents somewhere else so you can restore it when expanded again).
Here's a working example of option 1: http://jsfiddle.net/jfriend00/CTzsP/.
You'd need to either:
Put in a span/etc. after the first n words, and only hide that part, or
Change the viewable region, or
Replace or toggle the span/etc. with the "collapsed" view.
The last is a bit more customizable; using two separate elements allows trivial games to be played (showing an image, for example, like a little curly arrow) without modifying adding/removing DOM elements.
I tend towards the last because it's simple and obvious, but that's a personal preference, and really isn't as true as it used to be.
You can do some plugin authoring,I did a sample demo here ,based on your screenshot
<div class="toggle">ShowHide</div>
<div class="content">some content some content some content some content some content <br/> some content some content some content </div>
<div class="toggle">ShowHide</div>
<div class="content">some content some content some content some content some content <br/> some content some content some content </div>
here is javascript/jquery code
jQuery.fn.myToggle = function(selector, count) {
var methods = {
toggle: function(selector, count) {
if ($(selector).is(':visible')) {
var span = $('<span>');
span.text($(selector).text().substr(0, count) + "...");
span.insertAfter($(selector));
$(selector).hide();
}
else {
$(selector).show();
$(selector).next('span').hide();
}
}
};
$(this).each(function() {
methods.toggle($(this).next(selector), count);
$(this).click(function(evt) {
methods.toggle($(this).next(selector), count);
});
});
};
$(function() {
$('.toggle').myToggle('.content', 3);
});
Here is a solution using css properties only instead of mangling the dom.
http://jsfiddle.net/AYre3/4/
Now if you want some sort of animation happening as well you'll probably need to do a bit of measurement along the way.

Append div to end of document with jQuery?

I would like to write a function using jQuery to append a div to the end of a web page. I would like to be able to use the function on many different pages.
I've written the following code, but it doesn't work:
$(document).append('<div id="helloDiv"></div>');
$('#helloDiv').html('hello'); // does nothing
$('#helloDiv').css('top','100') // throws an error
How do I fix this?
It is meaningless to append to the document. Append to the document's body node instead, since this is where all the visible content is:
$(document.body).append('<div id="helloDiv"></div>');
If you want it to be at the end of the body, why not use:
$('body').append('<div id="helloDiv"></div>');
$('#helloDiv').html('hello');
$('#helloDiv').css('top', 100);
http://jsfiddle.net/ptuxX/
However, just .css('top', 100) does not do much unless you set the position to absolute.
Try:
$('body').append('<div id="helloDiv"></div>');
$('#helloDiv').html('hello');
$('#helloDiv').css('top', '100');
Or just add the content and it's css before adding the div.
var $newdiv = '<div id="helloDiv">hello</div>';
$('body').append($newdiv);

Categories