Is it possible to use jQuery to update CSS styles in an AJAX request

Is it possible to update the CSS of divs that are injected into files but do not exist in the main HTML file?

In my main HTML file, I have:

<div id="container">
<div id="page">
    <!placeholder>
</div></div>

I am having trouble with formatting due to the limitations of the code input system on this site.

Here is a snippet from my main.css file:

#container {
position: fixed;
margin-right: 0px;
overflow-x: hidden;
overflow-y: scroll;}
#page {
position: absolute;
width: 100%;
height: 1600px;
z-index: 10;
border-left: 1px solid #CCCCCC;}
#recposts {
position: absolute;
left:0;
top:1200px;}
.child {
height: 400px;
border-top: 1px solid #CCCCCC;
border-bottom: 1px solid #CCCCCC;
z-index: 11;
background-color: #EDEDED;
width: 100%;
padding-right: 10px;}

And here is what I'm injecting:

<div id ="page">    
<div id="recposts" class="child">
    <h1> Recent Posts  </h1> 
</div></div>

I need to change the position top of #recposts and the height of .child.

After some troubleshooting, I realized I needed to edit the CSS on HTML load. By putting the function into the .load() method, I was able to resolve the issue.

Answer №1

It seems like your question may be a bit unclear, but based on my understanding, you could potentially resolve it using the Jquery live() api. http://api.jquery.com/live/

With the Jquery live() api, you have the capability to modify newly added items as needed.

If you need to trigger a function call after an insertion, consider utilizing Jquery's DOM change() api. http://api.jquery.com/change/

For example:

$('#container').change(function() {
  $('#page').css("height",200);
});

EDIT: Due to limitations with the change() api on divs, here is the correct approach:

$('#container').bind("DOMSubtreeModified", function() {
  $('#page').css("height",200);
});

Answer №2

When the CSS rules are stored in a pre-loaded file like your main.css, any newly added elements thereafter (for example, from injected HTML) will inherit the styling specified by matching selectors in the CSS file. Does this align with what you were looking for?

Answer №3

Here is a simple solution that will insert a span with the class "someClass" into the div element with the id of "insertion".

CSS:

.someClass { color: red; }

HTML:

<div id="insertion"></div>

JavaScript:

<script>
 var newSpan = document.createElement("span");
 newSpan.className = "someClass";
 document.getElementById("insertion").appendChild(newSpan);
</script>

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

The Unexpected Flash: Troubleshooting an AJAX Request Gone Awry in CompoundJS

I am attempting to utilize the flash('error', 'error text') function in order to notify the webpage of any errors that may occur during an ajax request. The ajax request accesses an action that involves working with a database, making i ...

What is the reason behind the content of a div tag not hiding while the content of a p tag does

<html> <head> <title>How to make a div content disappear when clicked?</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <scrip ...

Utilizing the Replace function just once

I am currently using the Avada Theme on Wordpress and I am attempting to use jQuery to translate the social media privacy labels/content. Everything is working smoothly except for one issue. Below is the HTML code: function translate() { jQuery(".fus ...

Instructions for launching an Android app from a website

Is it possible to invoke my app from HTML? For instance, I am able to successfully call a webpage from my app using this code. android code: startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse( "myDomain.com"))); ...

What is the best way to position a small image within a menu?

Is there a way to adjust the position of the arrow image within the <li> element without affecting the layout of the unordered list? Below is the code snippet for reference: body { margin: 0; padding: 0; } nav { width: 100%; background: # ...

What is the comparable javascript code for the <script src="something1.json"></script> tag?

Within my HTML code, I currently have the following line to read a JSON file: <script src="something1.json"></script> Inside this JSON file, there is a structure like so: var varName = { .... } This method of definition naturally sets up ...

Unsuccessful Ajax call in Rails application

I'm a bit perplexed as to why my Ajax call is failing. It was previously working fine and I can't identify any changes that might have caused it to stop. When I initiate the call, an error pops up in Firebug's console pointing to this sectio ...

Retrieve data from the MySQL database that has not been previously accessed

Is there a way to retrieve rows that have not been previously loaded using ajax without relying on an id? This is because the sorting of rows is not done based on id. ...

What is the best way to retrieve the @Url.Action url from a distinct script file?

Currently, I have organized my JavaScript file separately from my web pages. One issue I am encountering is that I am unable to write the following line of code: var url = '@Url.Action("AddTrade", "DataService")'; I hesitate to hard-code the UR ...

How can we eliminate all elements from jQuery except for the first and second elements?

HTML <div class="geo_select"> <h3>header 3</h3> in Above HTML code i want to remove all element except <h3> and default content<div> inside the <div class='geo_select'> in jquery.. How to remove all ...

Facing issues with Handsontable opening within a jQuery UI dialog?

After implementing the Handsontable plugin in multiple tables, everything appears as expected on the parent page. However, when attempting to open a dialog containing a table, the tables do not display properly (only in IE). A demonstration of this issue c ...

Creating an HTML list that displays the UL Level numbers as the list values can be done by following these steps

Trying to create a styled HTML list with CSS that resembles the following: 1. List item     2. List item         3. List item         3. List item         3. List item   &nbs ...

When utilizing the browser's Back button, content negotiation is not being considered

Let's tackle the issue: I have developed a web application that responds to a request for a list of resources, such as: /items Initially, this request is made directly by the web browser through navigation to that path. The browser sends its standa ...

Retrieve the file path of an uploaded image in a Razor view using jQuery Ajax

I have been facing challenges while working on an ASP.NET MVC3 application where I am developing a custom image gallery. The browser being targeted is IE9, which rules out the option of using HTML5 File API. All my code resides within a strongly typed Razo ...

Using Node.js and the Jade templating engine, display the value of a passed variable

Asking such a basic question makes me feel guilty. app.get('/skumanagement/:id', function (req, res){ var options = req.params.id; // req.params.id = itemidx database.skuGetDetail(options, function (error, data){ winston.log('inf ...

Adjust the width of your table content to perfectly fit within the designated width by utilizing the CSS property "table width:

Example of a table <table> <tr> <td>Name</td> <td>John</td> <td>Age</td> <td>25</td> <td>Job Title</td> <td>Software Engineer ...

Extracting information from within Ajax's Jsonp

How can I retrieve data from the Ajax function(result)? Why isn't this app working? Please assist me. function star(a) { var res; $.ajax({ url: 'https://api-metrica.yandex.com/analytics/v3/data/ga?end-date=today&ids=ga%3A35 ...

Challenge with CSS Layering

I'm currently experiencing some issues with layering on my website. The problem at hand is that visitors are unable to interact with links within the div layers for some unknown reason. They can't click on the sidebar images or highlight text. I& ...

Adjust choices in a dropdown menu based on the selection of another dropdown menu

I am attempting to create a scenario where selecting an option from one dropdown list will dynamically change the options available in the next dropdown list. You can find my code on jsfiddle <!DOCTYPE html> <html> <body> &l ...

Instructions for sending an array of integers as an argument from JavaScript to Python

I have a JavaScript function that extracts the values of multiple checkboxes and stores them in an array: var selectedValues = $('.item:checked').map(function(){return parseInt($(this).attr('name'));}).get(); My goal is to pass this a ...