Using JQuery with CSS: I am looking to apply a specific CSS style class to the button within my table

I am struggling to apply a CSS style class to my button within a table.

There are two main issues I am encountering:

1) When I try to set the color/class using

 document.getElementById('btn11').style.color="blue";

The blue color only gets applied to the first row button.

2) How can I incorporate Google Material Design buttons in a variable?

 ex: var buttonVar = ('<md-button id="btn11">'+"Clicking"+'</md-button>');

Please advise

 success: function (data) {
     var trHTML = '';
     $.each(data, function (i, item) {

         if(data[i].test_progress=="Complete")
         {  

         var buttonVar = ('<button id="btn11">'+"Clicking"+'</button>');
         }
         else
             {
         var buttonVar = ('<button id="btn12">'+"Stopping"+'</button>');
             }


         trHTML += '<tr><td>' + data[i].test_type + 
         '</td><td>' + data[i].device_id + 
         '</td><td>' + data[i].make + '&nbsp'+data[i].model+
         '</td><td>' + data[i].stack_name +
         '</td><td>' + data[i].suite_name + 
         '</td><td>' + data[i].test_progress + '<br>&nbsp&nbsp&nbsp&nbsp'+data[i].tests_run+'/'+data[i].total_tests+
         '</td><td>' + data[i].start_time + 
         '</td><td>' + data[i].end_time +
         '</td><td>' + buttonVar +
         '</td></tr>';
        });
     $('#test1').append(trHTML);

      document.getElementById('btn11').style.color="blue";
//document.getElementById('btn11').style.className='md-primary md-raised';
        // $('btn11').addClass('action-button');

          $(document).on('click', '#btn11', function(event) {
             alert("The paragraph was clicked."+ i);
            });

          $(document).on('click', '#btn12', function(event) {
             alert("The paragraph was not clicked."+i);
            });

 }

Answer №1

Avoid using the same id for multiple elements on a page. Instead, assign the classes btn11 and btn12 to your buttons and use this JavaScript code:

document.getElementsByClassName('btn11').style.color="blue";

This will update the color of all buttons with the specified class.

A material design button can be created using a standard button (<button>) or link (a) with custom CSS styles. You can achieve this by using the materialize theme like so:

<a class="waves-effect waves-light btn">Stuff</a>

However, the specific implementation may vary depending on the CSS framework you are using.

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

Triangulating polygons on a spherical globe

Can all polygons be filled realistically? Take a look at this Codepen. It seems that the ThreeGeoJSON plugin is only able to create outlines for polygons, not fill them. I also experimented with using Earcut for triangulation. drawThreeGeo(data, radius, & ...

Encountering a script error when upgrading to rc4 in Angular 2

After attempting to update my Angular 2 version to 2.0.0.rc.4, I encountered a script error following npm install and npm start. Please see my package.json file below "dependencies": { "@angular/common": "2.0.0-rc.4", "@angular/core": "2.0.0-rc.4", ...

What is the proper way to send an AJAX response in C#?

As a newcomer to AJAX and C#, I am currently exploring how to integrate them seamlessly. In the process of building a web application, I am working on invoking a method from my C# code within an AJAX function. My goal is to receive a success message back ...

Manipulate Browser Navigation Behavior using JavaScript or AngularJS

How to Manage Browser Back Button Behavior Using AngularJS or JavaScript This is not a question, but rather a demonstration of how you can disable and manipulate the behavior of the browser's back button when using AngularJS or plain JavaScript. ...

Container Fluid height not fully supported in ASP.Net webpage

Despite attempting a solution from this source, I am still encountering an issue. My ASP.net page layout looks like this: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <%@ Page ...

Separate by individual words or symbols using regex

Can someone assist me in precisely breaking down the expression -(ab | c) & d -> (d|c)&e <-> f into individual elements within an array? ['-', '(', 'ab', '|', 'c', ')', &apos ...

Trouble with Bootstrap Popover's visibility

My current setup involves a popover that is not initializing properly. The code I am using is shown below: HTML: <div data-toggle="popover" data-placement="bottom" data-content="xyz">...</div> JavaScript: $(function () { $('[data-t ...

I am struggling to place the text within my <main> tag between my <header> and <footer> tags

I'm encountering an issue while developing a basic website. The text within the main tag is overlapping with the header tag. I've built simple webpages in the past without facing this problem. Isn't the text in the <main> tag supposed ...

Global installation of npm packages: Step-by-step guide

I've been interested in trying out Vue.js, but I'm struggling to figure out how to integrate it into my project. Is there a way to install the dependencies without requiring an internet connection to download them again, especially when they&apos ...

Preventing the display of AngularJS HTML tags while the app is being loaded

I am new to AngularJS (using version 1.5.8) and I am currently following the tutorials provided on docs.angularjs.org/tutorial. Below is the HTML code snippet: <div class="jumbotron"> <h1>{{application_name | uppercase }}</h1> ...

How to access a static variable in React Native within the same class

Recently transitioning from Android to React Native, I am encountering an issue with accessing variables within the same class. For example, when attempting to call URL_API_SERVER from another class, it returns 'undefined/api/v2'. class Constant ...

Saving events with a Full Calendar is a seamless process that

I am encountering a problem while trying to save the end time and enable dragging across multiple days on my fullcalendar. I have configured it to save data using jQuery.post to my database, but I am struggling to populate the end value and allow it to be ...

Issue with switching button and content positioned absolutely

Within this container, I have some content followed by a slide toggle button and then more content. The toggle button utilizes the jQuery function slideToggle. All elements within are positioned absolutely. [Introduction Content] Toggle Button [Hidden C ...

InvalidMethodError: Offspring cannot be invoked as a function

Provided below is the code for editing the onsubmit function in my create category javascript file. The objective is to capture user data and store it in a Firebase database. import React, { Component } from 'react'; import Dropzone from 'r ...

The current functionality of my web scraping code is operational; however, I am looking to enhance its specificity. Is it possible for me to designate which data to scrape by specifying the particular header above it

My current code is working perfectly, extracting all the <li> elements from a specific div. from scrapy.spider import BaseSpider from project.items import QualificationItem from scrapy.selector import HtmlXPathSelector from scrapy.http.request impor ...

At times, Express.js transfers control to the router

While developing a simple CRUD app using Node.js & Express.js, everything seemed to be working fine in my local environment. However, I encountered an issue when deploying it on a real server. Upon clicking the register button, the user registration proce ...

Get rid of the lower padding on a handsontable

I am currently using either Chrome Version 61.0.3163.100 (Official Build) (64-bit) or Safari Version 11.0 (12604.1.38.1.7) on Mac OS Sierra 10.12.6. I am looking to create a handsontable that may exceed the screen height: Click here for an example As I ...

Generate a binary string using JavaScript and then transform it into C#

I have an upload section in my JavaScript program. I utilize JS FileReader to obtain a binary string of the uploaded document before sending it to my C# WebApi for storage on the server. JavaScript Code let myFile = ev.target.files[0]; if(myFile.size > ...

The variable was not defined immediately after being assigned a value

Having some trouble with iterating through an array that is assigned to a variable in my code editor. Despite using console.log(variable_name.length) and successfully getting the array length, I keep getting an error saying the variable is undefined when t ...

Error occurs when page rendering is stuck in a recursive loop: Excessive re-renders detected

My webpage contains several form components as listed below. While everything on the front end seems to be working fine, I noticed that the Fetchmovies function is being called repeatedly and an error is thrown in the console: caught Error: Too many re-ren ...