Replace Div Content Onclick
I know nothing about jQuery but want to achieve something really important. I want to have a button/link that will replace the div content and if I press that button again so it wi
Solution 1:
Here's an approach: HTML:
<div id="1">
My Content 1
</div>
<div id="2" style="display:none;">
My Dynamic Content
</div>
<button id="btnClick">Click me!</button>
jQuery:
$('#btnClick').on('click',function(){
if($('#1').css('display')!='none'){
$('#2').html('Here is my dynamic content').show().siblings('div').hide();
}elseif($('#2').css('display')!='none'){
$('#1').show().siblings('div').hide();
}
});
JsFiddle:http://jsfiddle.net/ha6qp7w4/http://jsfiddle.net/ha6qp7w4/4 <--- Commented
Solution 2:
A simple addClass and removeClass will do the trick on what you need..
$('#change').on('click', function() {
$('div').each(function() {
if($(this).hasClass('active')) {
$(this).removeClass('active');
} else {
$(this).addClass('active');
}
});
});
Seee fiddle
I recommend you to learn jquery first before using.
Solution 3:
Working from your jsFiddle example:
The jsFiddle was fine, but you were missing semi-colons at the end of the event.preventDefault() statements.
This works: Revised jsFiddle
jQuery(document).ready(function() {
jQuery(".rec1").click(function(event) {
event.preventDefault();
jQuery('#rec-box').html(jQuery(this).next().html());
});
jQuery(".rec2").click(function(event) {
event.preventDefault();
jQuery('#rec-box2').html(jQuery(this).next().html());
});
});
Solution 4:
EDIT: This answer was submitted before the OP's jsFiddle example was posted in question. See second answer for response to that jsFiddle.
Here is an example of how it could work:
HTML:
<divid="someDiv">
Once upon a midnight dreary
<br>While I pondered weak and weary
<br>Over many a quaint and curious
<br>Volume of forgotten lore.
</div>
Type new text here:<br><inputtype="text"id="replacementtext" /><inputtype="button"id="mybutt"value="Swap" /><inputtype="hidden"id="vault" />
javascript/jQuery:
//Declare persistent vars outside functionvar savText, newText, myState = 0;
$('#mybutt').click(function(){
if (myState==0){
savText = $('#someDiv').html(); //save poem data from DIV
newText = $('#replacementtext').val(); //save data from input field
$('#replacementtext').val(''); //clear input field
$('#someDiv').html( newText ); //replace poem with insert field data
myState = 1; //remember swap has been done once
} else {
$('#someDiv').html(savText);
$('#replacementtext').val(newText); //replace contents
myState = 0;
}
});
Solution 5:
A Third Answer
Sorry, maybe I have it correct this time...
var savedBox1, savedBox2, state1=0, state2=0;
jQuery(document).ready(function() {
jQuery(".rec1").click(function() {
if (state1==0){
savedBox1 = jQuery('#rec-box').html();
jQuery('#rec-box').html(jQuery(this).next().html());
state1 = 1;
}else{
jQuery('#rec-box').html(savedBox1);
state1 = 0;
}
});
jQuery(".rec2").click(function() {
if (state1==0){
savedBox2 = jQuery('#rec-box2').html();
jQuery('#rec-box2').html(jQuery(this).next().html());
state2 = 1;
}else{
jQuery('#rec-box2').html(savedBox2);
state2 = 0;
}
});
});
Post a Comment for "Replace Div Content Onclick"