Updating the data-target attribute in Bootstrap using CSS (preferably) or JavaScript (as a secondary option)

        <!DOCTYPE html>
        <html lang="en">
        <head>
          <title>Bootstrap Example</title>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
          <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
          <style>
          @media screen and (min-width:500px)
          {
             .col-xs-4
                 {
                    text-align:center;
                 }
          }


            @media screen and (min-width:700px)
          {
             .col-xs-4
                 {
                    text-align:center;
                    width:100%;
                 }
          }



           .col-xs-4{
            text-align:center;
           }
           div .col-xs-4:hover{

           }
           .btn-primary{
            width:100%;
           }
          </style>

        </head>
        <body>

        <div class="container-fluid">
          <h1>Hello World!</h1>
          <p>Resize the browser window to see the effect.</p>
          <div id="">
            <div class="col-xs-4"  style="background-color:lavender;"><button type="submit"; data-toggle="collapse"; data-target="#doctorVisit"; class="btn btn-primary">Doctor Visit</button></div>
            <div id="doctorVisit" class="collapse">
          <table class="table"; width:500px; height:300px; >
            <thead>
              <tr>
                <th>Sr. No.</th>
                <th>Visited with</th>
                <th>Time</th>
                <th>edit</th>
              </tr>
            </thead>
            <tbody>
              <tr class="success">
                <td>John</td>
                <td>Doe</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="670d080f0927021f060a170b024904080a">[email protected]</a></td>
                <td>Doe</td>
              </tr>
              <tr class="danger">
                <td>Mary</td>
                <td>Moe</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4825293a31082d30292538242d662b2725">[email protected]</a></td>
                <td>Doe</td>
              </tr>
              <tr class="info">
                <td>July</td>
                <td>Dooley</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e3fce5f0c9ecf1e8e4f9e5eca7eae6e4">[email protected]</a></td>
                <td>Doe</td>
              </tr>
            </tbody>
          </table>
        </div>
            <div class="col-xs-4"  style="background-color:lavender;"><button type="submit"; id="pharmacyVisitBtn" data-toggle="collapse"; data-target="#pharmacyVisit"; class="btn btn-primary">Pharmacy Visit</button></div>
            <div id="pharmacyVisit" class="collapse">
          <table class="table"; width:500px; height:300px; >
            <thead>
              <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Email</th>
                <th>edit</th>
              </tr>
            </thead>
            <tbody>
              <tr class="success">
                <td>John</td>
                <td>Doe</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91fbfef9ffd1f4e9f0fce1fdf4bff2fefc">[email protected]</a></td>
                <td>Doe</td>
              </tr>
              <tr class="danger">
                <td>Mary</td>
                <td>Moe</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="670a06151e27021f060a170b024904080a">[email protected]</a></td>
                <td>Doe</td>
              </tr>
              <tr class="info">
                <td>July</td>
                <td>Dooley</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e68c938a9fa6839e878b968a83c885898b">[email protected]</a></td>
                <td>Doe</td>
              </tr>
            </tbody>
          </table>
        </div>
         <div class="col-xs-4"  style="background-color:lavender;"><button type="submit"; data-toggle="collapse"; data-target="#stockistVisit"; class="btn btn-primary">Stockist Visit</button></div>
            <div id="stockistVisit" class="collapse">
          <table class="table"; width:500px; height:300px; >
            <thead>
              <tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Email</th>
                <th>edit</th>
              </tr>
            </thead>
            <tbody>
              <tr class="success">
                <td>John</td>
                <td>Doe</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e141116103e1b061f130e121b501d1113">[email protected]</a></td>
                <td>Doe</td>
              </tr>
              <tr class="danger">
                <td>Mary</td>
                <td>Moe</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b4b8aba099bca1b8b4a9b5bcf7bab6b4">[email protected]</a></td>
                <td>Doe</td>
              </tr>
              <tr class="info">
                <td>July</td>
                <td>Dooley</td>
                <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="79130c1500391c01181409151c571a1614">[email protected]</a></td>
                <td>Doe</td>
              </tr>
            </tbody>
          </table>
        </div>
          </div>
        </div>

        <script>
        function show()
        {
         alert ("hey");

        }
        </script>



        </body>
        </html>

Ensure you run the above code using a server for proper functionality.

Explanation: I am looking to modify the 'data-target' attribute to target the div with the ID #stockistVisit when the screen size is small. Can this be achieved using CSS?

If not possible with CSS, is it feasible to achieve this using JavaScript?

I am also utilizing AngularJS. Is there a way to accomplish this using that framework as well?

Answer №1

When it comes to adjusting data attributes based on media queries, JavaScript is the way to go (see jQuery example below).

$(window).resize(function() {
    if ($(this).width() < 500) $('target-here').data('color', 'red');
    else $('target-here').data('color', 'blue');
});

The .width() method provides the window width in pixels, allowing you to dynamically modify data attributes as needed based on specific conditions.

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

Click twice on the editable AngularJS table to wrap each item

As a new learner of Angularjs, I found an existing code example and decided to customize it. My goal was to enable double-click editing for each li item. However, after adding one more li, the functionality did not work as expected. <li ng-dblclick="st ...

What is the correct method for verifying an array within an AngularJS form?

I am dealing with a form that contains an array of products: [{"productId":"12121212","count":5},{"productId":"22222222","count":6}] My goal is to: Validate the form on the client side and disable submission if any product array fields are invalid. Ca ...

Organize an array of objects with underscore.js to arrange them in a

I have an array of objects that looks like this: profData=[{"Details":{"CODE":"PAT4PAS","DESCRIPTION":"PASTIE 4N20 12 X 175G","LOCATION":"FREEZER","UNITS":"BOX","WAREHOUSE":"00","AVAILABLE":"15.0000","ON_HAND":"15.0000","BRAND":"4N20","PRICE1":"18.80"," ...

Can you explain the significance of the ColSpan property in the Material UI TablePagination?

Why is ColSpan used in this code snippet? Reference: https://material-ui.com/components/tables/#table Check for the arrow symbol <TableFooter> <TableRow> <TablePagination rowsPerPageOptions={[5, ...

Constance in JavaScript

Looking to create constants in Javascript and seeking advice on the best way to do so. While I am aware that true constants don't technically exist, I'm finding it difficult to change values after exporting them. Are constants necessary? Is the ...

Capture a snapshot of a webpage that includes an embedded iframe

Currently, we have a nodeJS/angular 4 website that contains an iframe from a third party (powerBI Emebdded). Our goal is to develop a feature that allows the end user to capture a screenshot of the entire page, including the content within the iframe. We ...

The functionality of core-ui-select is not functioning properly following the adjustment of the

I've implemented the jquery plugin "core-ui-select" to enhance the appearance of my form select element. Initially, it was functioning perfectly with this URL: However, after applying htaccess to rewrite the URL, the styling no longer works: I&apos ...

"Experience the sleek elegance of Aftershow with the dynamic features of

I encountered a similar issue as the individual in this post: ShareThis plugin not working in FancyBox title. However, I have managed to resolve it now, except for the afterShow: function(){ stButtons.locateElements(); } Could someone advise me on where ...

Trying to update the +/- icon on an accordion dropdown, however, the local HTML file is not loading the JavaScript file. (Using Bootstrap 4)

I'm completely new to JS and struggling a bit, so please be patient. I've been searching online for a few days now without much luck. I'm using Bootstrap 4 and trying to create an accordion with a +/- icon that changes when expanded or colla ...

SinonJS - Retrieving Property Value Prior to Stub Invocation

Currently leveraging sinon.js for stubbing functionalities where it is feasible to stub and spy on methods but not properties based on my observations. I'm interested in knowing if there's a way to verify whether state.searchText gets assigned t ...

The appearance of a Kendo dialog box in Angular 2+ inside a Kendo grid is triggered when enclosed within a kendoGridDetail

Is there a CSS method to display the Kendo Dialog outside of the kendo grid when it is wrapped inside kendoGridDetailTemplate? Here's how my component currently appears: <kendo-grid><div *kendoGridDetailTemplate="let dataItem"> <looku ...

Is there a way to swap out multiple characters within a string when using ng-repeat in AngularJS?

How can I replace multiple characters in a string using ng-repeat in AngularJS? I've tried the following code, but it's not working. I need to remove #, _, and . from the strings in my list. How can I achieve this in AngularJS? <body> &l ...

Tips on successfully downloading a large number of remote files using Node.js HTTP GET method without encountering any issues

I need to download a long list of files generated by an internal processing system using the HTTP get method in node js. While downloading single or a few files works fine, I am encountering timeouts and errors when trying to download a large number of fil ...

Incorporating Google Analytics into a personalized Order Confirmation page on Woocommerce 3

I have a personalized gratitude page for post-checkout in WooCommerce where I want to include order details in a Google ecommerce tracking tag to log the sale in analytics. Part of this involves adding the following information for each item in the order.. ...

How to transform a buffer into a string that is easily readable using

I am trying to convert a JSON buffer into a readable or JSON object, but none of the techniques I have tried so far (such as JSON.stringify(), toString('utf8')) seem to be working for me. This is what my code looks like: https://i.sstatic.net/x ...

What is the process for redirecting to an external URL while including post parameters?

When a user accesses my endpoint, I need it to automatically redirect them to an external URL with URL-encoded form parameters. Endpoint: https://example.com Form Parameters: Name: testing ID: 123456 I have attempted the following in Express. It succes ...

How to access XML tags that are repeated at multiple levels of hierarchy using JavaScript

I'm working with an XML file that has a specific hierarchy structure as shown below: item item (item details) item (item details) item item (item details) item (i ...

Tips on displaying a selected choice | Utilizing Material UI Autocomplete

I have successfully fetched data from an API and used it as options in Material UI Autocomplete. However, when I select an option and send it back to the API using a POST request, the selected category is displayed as a number (0) instead of text, as shown ...

The compilation process encountered an error: TypeError - Unable to access property 'exclude' as it is undefined (awesome-typescript-loader)

After successfully converting my existing Angular 2 project into Angular 4, I encountered the following error: Module build failed: TypeError: Cannot read property 'exclude' of undefined For more details, please refer to the attached image bel ...

Managing Data Types in AJAX Requests

Can you help me figure out why my AJAX call is not reaching success after hours of troubleshooting? It seems like the issue lies in the dataType that the AJAX call is expecting or receiving (JavaScript vs JSON). Unfortunately, I'm not sure how to addr ...