PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How do I refresh div contents on button click

 September 08, 2022     ajax, javascript     No comments   

Issue

I have a script inside a div which I want to refresh when someone clicks a button with a unique ID.

I have some data which will be displayed inside the div - which I need to refresh without refreshing the whole page.

I've found a solution which changes a border colour but I can't quite simplify it to what I need. Here's what I have so far

var storeColor = 'red';

$('#container').on('click', 'button', function() {
  var $p = $('#content p');
  var tmp = $p.css('border-color');
  $p.css('border-color', storeColor);
  storeColor = tmp;
});
</div>
  <button type="button">Refresh DIV</button>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="container">
  
  Contents of div
    
 </div>
    
    
    
  


Solution

I think there are a few problems with your code... The button is outside of the container and thats the reason the click listener doesn't work. Inside the click function you try to access a paragraph p inside some element with the id content which doesn't exist in your code

var storeColor = 'red';

$('#btnRefresh').on('click', function() {
  var $p = $('#container');
  $p.css('background-color', 'red');
  $p.html('hello world');
});
  <button type="button" id="btnRefresh">Refresh DIV</button>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="container">
  
  Contents of div
    
 </div>
    



Answered By - Michael
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing