What is the best way to determine the number of rows various div elements occupy within a wrapper using javascript?

The reference for this code snippet can be found at http://jsfiddle.net/4fV3k/

I have a function called SetGridBorder that takes a border style parameter like 1px solid red and a selector of the wrapper element such as box-wrapper.

In my example, there are 4 rows in a row, resulting in 4 columns and 4 rows. How can I determine this using JavaScript and apply the border according to these rules?

  1. The second and third cells in the top row should only have a missing left and right border (no duplicated border).

  2. The second and third columns in the middle rows should only have a missing top and bottom border (no duplicated border here either).

Is there a suggested approach or better way to handle this in JavaScript? Any recommendations on how to improve it?

$(document).ready(function () {   
    var box_wrapper = $(".box-box-wrapper", ".box");   
    SetGridBorder(4,4)
});

function SetGridBorder(style,selector) {

}​

Answer №1

To determine the number of rows and columns, divide the wrapper's width and height by the box's width and height, respectively. In this scenario, the wrapper's height was initially zero, so I included overflow:auto; in the body .box-wrapper class. Check out the updated fiddle at http://jsfiddle.net/4fV3k/7/

function getRows() {
    var wrapperWidth = $(".box-wrapper").width();
    var boxWidth = $(".box-wrapper > div").width();
    return Math.floor(wrapperWidth / boxWidth);
}

function getColumns() {
    var wrapperHeight = $(".box-wrapper").height();
    var boxHeight = $(".box-wrapper > div").height();
    return Math.floor(wrapperHeight / boxHeight);
}

Answer №2

Just an FYI.

 body .container {
  width: 200px;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
    border:solid 2px #333;
}
$(document).ready(function () {
    $(".container").each(function(index, element) {
        if((index+1)%5!=0){
            $(element).css({'border-right':'none'})        
        }
        if((index+1)>6){
           $(element).css({'border-top':'none'})        
        }
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activate Popover on Third Click with Bootstrap 4

Is it possible to create a button that triggers a popover after 3 clicks and then automatically dismisses after one click using Bootstrap v4.3.1? Code: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.mi ...

Tips for stopping Vue.js automatic merging of CSS classes

Recently, I embarked on my journey with Vue.js and have been thoroughly enjoying the experience. However, I've stumbled upon a challenge that has me stumped. Despite searching high and low and studying the documentation, I haven't found a solutio ...

I encountered a "TypeError: Unable to access property 'name' of undefined" error when attempting to export a redux reducer

UPDATE: I encountered an issue where the namePlaceholder constant was returning undefined even though I was dispatching actions correctly. When attempting to export my selector function, I received an error: https://i.sstatic.net/MwfUP.png Here is the c ...

Creating dynamic parameters in Nuxt JS for nested pages

Struggling with creating a dynamic param for my nuxt JS page's correct folder structure. The URL I'm aiming for is similar to: https://example.com/landing/123 The number 123 is the dynamic parameter I want to retrieve using this.$route. I&apos ...

What sets Vuex apart from the Event Bus mechanism?

In my quest to grasp the intricacies of component communication, a question has surfaced: What sets Vue's event bus strategy apart from Vuex in handling inter-component communication? Additionally, when is the optimal moment to employ each technique a ...

One way to store form data in a database without triggering form submission with socket.io

Question: I am facing a dilemma on how to save form input data to a MySQL database while also using socket.io, as the preventDefault function seems to be causing issues. My current setup involves submitting form data via post requests and then storing it i ...

Issue regarding alignment in quill-image-resize-vue

There is an issue with the alignment of images when using "quill-image-resize-vue" - they are supposed to align to the center or right, but end up being left aligned in the result. This inconsistency does not occur in all cases, and it's been challen ...

What could be causing htmlparser2 in Node.js to add extra nodes during document parsing?

Seeking to extract the HTML content of a webpage using node so I can process its DOM to create something unrelated. It's crucial to accurately obtain the DOM representation from the HTML in order to properly process it. Utilizing htmlparser2 for this ...

Parent component in Material-ui theme distinguishing itself from child component of the same type

I'm working on customizing the styling of a Material UI Expansion Panel, specifically to apply my theme only to the main parent panel when it is expanded, and not to any nested panels within it. For instance: https://i.sstatic.net/nBhcN.png Here&ap ...

Vue Websockets twofold

I am experiencing some issues with Laravel/Echo websockets and Vue.js integration. I have set up everything as required, and it works, but not quite as expected. The problem arises when I refresh the page and send a request - it displays fine. However, if ...

When the screen size changes, the Bootstrap 4 h-100 column does not stack correctly

I've encountered an issue with Bootstrap 4 while utilizing the h-50 and h-100 helper classes along with rows incorporating columns of different breakpoints. Consider the following scenario : <style> html,body { height: 100%; } ...

What are the steps to create a connect4 board featuring rounded corners and curved sides?

How can I create a Connect4 board with the exact styles and properties shown in the image? I want to achieve the curved sides effect as displayed. Can this be done using only HTML elements, or is there an easy SVG solution available? Here is my current co ...

"Exploring locations with Google Maps and integrating them into interactive

I recently encountered an issue while working on a node express application and integrating the google maps javascript api. The problem arose when I attempted to transfer the sample code from the google website, along with my API key, into a .ejs file. S ...

Discover the ins and outs of integrating YAML front matter into your destination directory path

I am looking to customize the path of my blog posts to include a fancy date format like /blog/2013/09/17 so that the links from my previous octopress blog remain intact. Within the YAML front matter on each markdown page, I have included the date informat ...

Adjusting the width of a kendo UI themed text field using CSS

Isn't it fascinating how you can adjust the width of a text box like this? @Html.TextBoxFor(m => m.LoginId, new { @class = "k-textbox", style="width:400px" }) However, I am seeking a more elegant solution. I attempted to implement this in a fres ...

How can I make the outer function in AJAX's onreadystatechange function return 'true'?

Within my Javascript/AJAX function below, I am striving for a return of true or false: function submitValidate() { var xmlhttp; xmlhttp = null; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari try { xmlhttp ...

Field cannot be submitted without the required input after an Ajax request

I want to ensure that all my text/email input forms require a submission before you can "Submit" the email. Unfortunately, when using Ajax to prevent the page from refreshing after pressing the button, the required attribute does not function properly. T ...

create a JavaScript array variable for posting items

My goal is to use this javascript to make four posts for the 'var msg' array. However, it currently posts 'encodeURIComponent(msg[i])' four times instead. How can I resolve this issue? var msg = ['one', 'two& ...

What are the steps to manipulate a scene using three.js?

This HTML page showcases a scene with various points that can be comfortably zoomed in using the mouse wheel. However, I am looking to implement the functionality to drag the scene after zooming in. The idea is to click and hold the left mouse button whil ...

The Google font feature is causing an issue where text cannot be selected when trying to print a

I am in the process of creating a Vue application to generate my CV in PDF format. I have incorporated the Google font Montserrat into the project, but when I attempt to print the page to file (CTRL + P in Firefox), the text appears as if it were an image ...