Let I've two divs on my page which I want to set the same width. Consider the following markup:
<div>
<div>
<!-- content -->
<div id="a">
</div>
</div>
</div>
<div id="b">
</div>
How can I set the width of div#b equal to the computed width of div#a
var width_a = document.getElementById("a").offsetWidth; //get the width of div#a;
document.getElementById("b").style.width = width_a + 'px' ; //set the width of div#b to width ofdiv#a
Just Simply do this
document.getElementById("a").offsetWidth=document.getElementById("b").offsetWidth;
this will work
$('#b').width($('#a').width());
By using jQuery you can set as below:
$('div#b').width($('div#a').width())
Working Example: JSFiddle Demo
Using Javascript:
document.getElementById('b').style.width = document.getElementById('a').offsetWidth + 'px';
Using jQuery:
$('div#b').css({
width: $('div#a').width()
});
Related
I would like to match the height of class "project_region" each 4 elements,
<div class="grid_new inner_each_pr" onclick="location.href='/region/agroproject/33'">
<div class="inn_pr">
<p class="project_region"><b>Московская область</b><br>Животноводство, Строительство сельскохозяйственных объектов | Молочное скотоводство</p>
<p class="project_name">Строительство животноводческой фермы</p>
<div class="info_project_down">
<div class="block_rub inline">
<p>406 <span>млн. р.</span></p>
</div>
<div class="block_time">
<p>8<span>лет</span></p>
</div>
</div>
</div>
</div>
The HTML-Code above is inside a class="row", and all class="inner_each_pr" are set inline, I tried to do, but it get's me the max height of all elements, I need need to set max height each 4 element.
Any idea?
Try
$(document).ready(function() {
var maxHeight = -1;
$('.inner_each_pr').each(function() {
maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
});
$('.inner_each_pr').each(function() {
$(this).height(maxHeight);
});
});
check this link for details Div is mysteriously moving down after displaying first 3 rows properly
here i am going to ask two questions...
how to get the exact width of the div
Code:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Width of div: " + $("#a").width());
});
});
</script>
</head>
<body>
<div id="a" style="width:1000px;height:500px;">
<div style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;">
</div>
</div>
<br>
<button>Display the width of div</button>
</body>
</html>
here i am declared 2 .. parent div width is 1000 child div width is 300 if i use $("#a").width() function i will get 1000 which i mentioned on that div. but how can i get the exact used width(300) using $("#a") as well as same in height?
your "a" is 1000px - since you want the width of the div within the "a" you need to use:
$("#a div").width()
or if you can have multiple divs - then use :first (or some other selector)
Try with chaining in jquery
$("#a div").width()
You can use attr methods like
$("button").click(function(){
alert("Width of div: " + $("#a div").attr("width"));
});
Use can use this code to get the height and width div inside #a div
$(document).ready(function(){
$("button").click(function(){
alert("Width of div: " + $("#a div").width());
alert("Width of div: " + $("#a div").height());
});
});
If you would have only two divs, then why dont you just assign id to your second div also.
<div id="a" style="width:1000px;height:500px;">
<div id="b" style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid blue;background- color:lightblue;">
</div>
</div>
Then you can just get the width and height as follows:
var width = $('#b').attr("width");
var height = $('#b').attr("height");
I have four divs with different IDs. How would I set height for all these divs?
<div id="slide-1">a</div>
<div id="slide-2">b</div>
<div id="slide-3">c</div>
<div id="slide-4">d</div>
I know we can do something like this below, but there are other divs too before this.
$('div').height($(window).height());
I want to set these four divs to window height alone. I can't hard-code here as the slides may vary.
this will select all the divs that their ids start with "slide-" and set the height to window height:
$('div[id^="slide-"]').height($(window).height());
$('div[id^="slide-"]').css("height", $(window).height() + 'px');
Demo:
http://jsfiddle.net/463KU/1/
DEMO
$('div').filter(function() { return /^slide-[1-4]$/.test( this.id ); })
.height( $(window).height() );
if you like to increase the hieght of specific divs use class for multiple
<div class="divHeight" id="slide-1">a</div>
<div class="divHeight" id="slide-2">b</div>
<div class="divHeight" id="slide-3">c</div>
<div class="divHeight" id="slide-4">d</div>
$('.divHeight').height($(window).height());
I also recommended you use this way to get window height:
var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
<div class="divHeight" id="slide-1">a</div>
<div class="divHeight" id="slide-2">b</div>
<div class="divHeight" id="slide-3">c</div>
<div class="divHeight" id="slide-4">d</div>
$('.divHeight').height(windowHeight);
I have a div structure as follows:
<div id="showHide">
<div>Alarm</div>
<div>Alarmasdf</div>
<div>Alarmasdffasdff</div>
Is there any way that I can get the width of the content like (Alarmasdffasdff). This content is the largest.
I am able to get the length of the content, but not the width.
Please suggest.
Assuming the div elements have been changed to display: inline, the width of the element will match the content.
If this is not the case I would suggest wrapping the content in a span, or any other suitable inline element, and getting the width of that. For example:
<div id="showHide">
<div>
<span>Alarm</span>
</div>
<div>
<span>Alarmasdf</span>
</div>
<div>
<span>Alarmasdffasdff</span>
</div>
</div>
$('#showhide').find('div:last span').width();
The Width of all of your 'div's is 100% regardless of their content. This is how block level elements behave...
use :contains()
$( "#showhide div:contains('Alarmasdffasdff')").width();
As said by others, width of a block level element is 100% regardless of its content's width. So first you need to change the display of the div to inline-block
<style>
#showHide div
{
display:inline-block;
}
</style>
<div id="showHide">
<div>Alarm
</div>
<div>Alarmasdf
</div>
<div>Alarmasdffasdff
</div>
</div>
$('#showhide>div:contains("Alarmasdffasdff")').innerWidth();
Jquery makes it very easy.
$('#showHide').find('div:last').css('width');
css() returns the computed style of the element, which is the inline styling as well as default styling and any other css selector.
Without jquery it goes a little less "clean"
var mystyle = window.getComputedStyle ? window.getComputedStyle(myobject, null) : myobject.currentStyle;
mystyle.width shows the calculated width, where myobject is the element you want to check.
html :
<div id="showHide">
<div>Alarm</div>
<div>Alarmasdf</div>
<div>Alarmasdffasdff</div>
</div>
script :
var lastDiv = $('#showHide').find('div').last();
var lastDivWidth = lastDiv.width();
alert(lastDivWidth);
jsFiddle
<div id="showHide">
<div class="content" style="width:50px" >Alarm</div>
<div class="content" style="width:60px">Alarmasdf</div>
<div class="content" style="width:120px">Alarmasdffasdff</div>
</div>
Custom width is given so that u can understand the difference, else every div will be having same width, and class content is added for convinience of processing
findWidth("Alarmasdf"); // Calls the function below
function findWidth(value)
{
var width=0;
$(".content").each(function(e){
if($(this).text()==value)
{
width = $(this).width();
}
});
alert(width);
}
Check the fiddle here
http://jsfiddle.net/46LH9/
I'm trying to count the total number of img elements and then the number of style tags present in a parent div tag using jquery. The current HTML looks like this:
<div id="origin" class="ui-test">
<div class="polaroid ui-test">
<img src="http://example.com/84655/l/72105d6f46205a948f00f6e59c299930.jpg" >
</div>
<div class="polaroid ui-test">
<img src="http://example.com/77676/l/819e2093be4c61d2e21b1175f9d0f0f9.jpg" >
</div>
<div class="polaroid ui-test" style="left: 288.328125px; top: -8px; display: none;">
<img src="http://example.com/47901/l/79f936ef9847793254bad21e16b2448f.jpg" >
</div>
<div class="polaroid ui-test" style="left: 94.328125px; top: 6px; display: none;">
<img src="http://example.com/49761/l/6d10064d6b189c6934fd264ac295f5f8.jpg" >
</div>
</div>
Based on the above example I'm trying to get a count of total img elements under the parent origin container and then how many are showing display: none;
I would expect to see: 4 img tags total and 2 hidden img tags
I think I can get the toital number of images with the following:
var totalImages = $("#origin img").length;
How do I get the total number of images showing display:none?
Try this:
var totalImages = $("#origin img").length;
var totalImagesWithStyle = $("#origin div[style] img").length;
//or
var totalImagesWithStyle = $("#origin div:hidden img").length;
//or
var totalImagesWithStyle = $("#origin .polaroid:hidden img").length;
Fiddle
Try using the :hidden jQuery selector.
$('#origin img:hidden');
See http://jsfiddle.net/35uvZ/. It consists of your HTML part; I left the left and top declarations. Other than that just some jQuery:
var $imgWrp = $('#origin .polaroid');
var $images = $imgWrp.find('img');
var $hiddenImages = $imgWrp.filter(':hidden').find('img');
console.log($images.length);
console.log($hiddenImages.length);
The :hidden selector selects all elements that are hidden.
$( "#origin div:hidden" ).length;
For efficiency, I would break it up into two steps:
var allImages = $("#origin").find("img");
var hiddenImages = allImages.filter(":hidden");
Then you could retrieve the lengths of either or both sets (as needed) and proceed from there.