the idea is list and grid classs.
example code:
$(document).ready(function(){
$(".cfredcf").on('click', function() {
$('.course-title-cf').removeClass('cfbluecf-p');
$('.course-title-cf').addClass('cfredcf-p');
});
$(".cfbluecf").on('click', function() {
$('.course-title-cf').removeClass('cfredcf-p');
$('.course-title-cf').addClass('cfbluecf-p');
});
});
.cfredcf-p{
color:red;
}
.cfbluecf-p{
color:blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="cfredcf" style="color:red;">red</p>
<p class="cfbluecf" style="color:blue;">blue</p>
<hr />
<p class="course-title-cf">Title Example</p>
<p class="course-title-cf">Title Example</p>
<p class="course-title-cf">Title Example</p>
<hr />
the code working fine until now but when get more (Title Example) from the database by ajax and already choose red the new results is not coming with red i must click on red again !.
the new title or results comming from database:
it will be like that
<hr />
<p class="course-title-cf">Title Example</p>
<hr />
but not geting withe new add class i must click agin to add.
In fact, it would be more effective to add (AJAX) codes and see the scenarios, but,
A lot of solutions come to mind, but if you ask what is the smartest, fastest and easiest one, I would make a <div> element that wraps all these <p> elements. And until I color each <p>, I would do <div class = "cfredcf-p / cfbluecf-p">. Then my css is .cfredcf-p p {color: red;} I would rearrange it as.
$(document).ready(function() {
$(".cfredcf").on('click', function() {
$('.course-title-cf').removeClass('cfbluecf-p');
$('.course-title-cf').addClass('cfredcf-p');
});
$(".cfbluecf").on('click', function() {
$('.course-title-cf').removeClass('cfredcf-p');
$('.course-title-cf').addClass('cfbluecf-p');
});
$("button").on('click', function() {
$('.course-title-cf').append("<p>Title Example</p>");
});
});
.cfredcf-p p {
color: red;
}
.cfbluecf-p p {
color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="cfredcf" style="color:red;">red</p>
<p class="cfbluecf" style="color:blue;">blue</p>
<button>Add new</button>
<hr />
<div class="course-title-cf">
<p>Title Example</p>
<p>Title Example</p>
<p>Title Example</p>
</div>
<hr />
Thus, every new <p> element inserted inside the wrapper element will take the same color.
Related
I am trying to use a toggle switch inside a tab control, to change the display of data for the current tab. So I have 3 tabs (Plots, People and Address). Tabs 2 & 3 are easy as they are just plain text content. But tab 1 (Plots) I need to filter the display of data.
So I broke this down to try and get two buttons to hide and display the divs as I want them to, but they both just hide everything.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$("#current").toggle();
});
});
$(document).ready(function() {
$("button").click(function() {
$("#all").toggle();
});
});
</script>
</head>
<body>
<!--<div>
<input id="current" type="radio" name="group1" value="plots">Current Plots
<br>
<input id="all" type="radio" name="group1" value="plots1">All Plots
</div>-->
<button class="current">Toggle Current Plots DIV</button>
<button class="all">Toggle all plots DIV</button>
<hr>
<div class="current" id="current">
<p>This is a paragraph with little content.</p>
<p>Paragraph 1 - Current Plots.</p>
<hr>
</div>
<div class="all" id="all">
<p>This is another small paragraph.</p>
<p>Paragraph 2 - All Plots</p>
<hr>
</div>
</body>
</html>
It's because for your click event you are using button as a selector, so any button element is going to toggle everything. Change the selector to the class on the button.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
// This changed from button
$(".current").click(function() {
$("#current").toggle();
});
});
$(document).ready(function() {
// This changed from button
$(".all").click(function() {
$("#all").toggle();
});
});
</script>
</head>
<body>
<!--<div>
<input id="current" type="radio" name="group1" value="plots">Current Plots
<br>
<input id="all" type="radio" name="group1" value="plots1">All Plots
</div>-->
<button class="current">Toggle Current Plots DIV</button>
<button class="all">Toggle all plots DIV</button>
<hr>
<div class="current" id="current">
<p>This is a paragraph with little content.</p>
<p>Paragraph 1 - Current Plots.</p>
<hr>
</div>
<div class="all" id="all">
<p>This is another small paragraph.</p>
<p>Paragraph 2 - All Plots</p>
<hr>
</div>
</body>
</html>
Edit
Adding a more efficient way to handle by creating one function than can handle the buttons using data attributes on the button elements.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Use a data attribute on the button to toggle an element's id.
$('button').click(function() {
// get the button's data-target value
let target = $(this).data('target');
// use the data-target value to pass as the id of the target element.
$("#" + target).toggle();
});
});
</script>
</head>
<body>
<button data-target="current">Toggle Current Plots DIV</button>
<button data-target="all">Toggle all plots DIV</button>
<hr>
<div id="current">
<p>This is a paragraph with little content.</p>
<p>Paragraph 1 - Current Plots.</p>
<hr>
</div>
<div id="all">
<p>This is another small paragraph.</p>
<p>Paragraph 2 - All Plots</p>
<hr>
</div>
</body>
</html>
I placed a slider in my blogspot using bxslider but now the link (the title of the book) is not working. See phlawdigest.blospost.com to see the actual site. Below are the codes I used.
The Jquery Code is as follows:
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://bxslider.com/lib/jquery.bxslider.css" type="text/css" />
<script src="http://bxslider.com/lib/jquery.bxslider.js"></script>
<script>
$(document).ready(function() {
$('.image-left').bxSlider({
auto: true,
pause: 50000,
// in millisecond
autoHover: true, // pause on hover
autoControls: true,
captions: false,
});
});
HTML code is as follows:
<div class="image-left">
<div>
<img src="URL link/>
<h2> Book Title One</h2>
<p>some text description</p>
</div>
<div>
<img src="URL link"/>
<h2> Book Title Two</h2>
<p>some text description</p>
</div>
</div>
bxSlider support jquery3.1.1 and you can not give appropriate css properties for your html class...
<!DOCTYPE html>
<html>
<head>
<title>Try jQuery Online</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css">
<style>
.selected {
color: red;
}
.highlight {
background: yellow;
}
.image-left {
text-align: center;
}
img
{
width: 100%;
height: 320px;
}
.bx-wrapper .bx-controls.bx-has-controls-auto.bx-has-pager .bx-pager
{
text-align: center !important;
}
</style>
</head>
<body>
<div class="image-left">
<div>
<img src="https://images.pexels.com/photos/50594/sea-bay-waterfront-beach-50594.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/>
<h2>
Book Title One
</h2>
<p>some text description</p>
</div>
<div>
<img src="https://images.pexels.com/photos/531321/pexels-photo-531321.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"/>
<h2> Book Title Two</h2>
<p>some text description</p>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.min.js"></script>
<script>
$(document).ready(function() {
$('.image-left').bxSlider({
auto: true,
pause: 50000,
touchEnabled: false,
// in millisecond
autoHover: true, // pause on hover
controls: true,
captions: false,
});
});
</script>
</body>
</html>
syntax error:
<img src="URL link/> <------missing last quotation. Was that a typo when proving the demo code, or is it missing on the actual code? Because that would stop it working.
I want to have a simple read more and read less button functionality where if one button is clicked then it will show the text and when another is clicked it will hide the previously opened and so on but I have been unsuccessful in changing the button text as each is changed e.g. when it says read more and when I click it changes to read less, then when I click another button it is still displaying read less.
Here is my example of what I have done so far https://codepen.io/Niall_Caffrey/pen/abNavaj
$('body').on('click', '.shownow', function() {
$(this).parents('.main').find('.notshow').addClass('nohide').toggle();
$('.notshow').each(function() {
if (!$(this).hasClass('nohide')) {
$(this).hide();
} else {
$('.notshow').removeClass('nohide');
}
});
if($(".notshow").is(":hidden")){
$(this).text("Read More");
$('.notshow').addClass('showhide');
$('.main').addClass('showhide');
};
if($(".notshow").is(":visible")){
$(this).text("Read Less");
$('.notshow').removeClass('showhide');
$('.main').removeClass('showhide');
};
});
.notshow {
display: none;
}
.main{
width:25%;
float:left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>How it is done</h3>
<div class="main">
<div class="notshow">
<p>Lorem Ipsum
</p>
</div>
<div class="title">
<div class="shownow">
<p class="btn">Read More</p>
</div>
</div>
</div>
<div class="main">
<div class="notshow">
<p>lorem Ipsum</p>
</div>
<div class="title">
<div class="shownow">
<p class="btn">Read More</p>
</div>
</div>
</div>
<div class="main">
<div class="notshow">
<p>Lorem ipsum</p>
</div>
<div class="title">
<div class="shownow">
<p class="btn">Read More</p>
</div>
</div>
</div>
The problem is how to make clickable/togglable buttons that correctly both hide the text and relabel the button of any previously clicked button.
I am not familiar with jquery and I couldn't completely follow the given code, except to note that the writing of 'Read More' was going to 'this' which is the most recently clicked element, not the previous one. I also noted that the way 'Read More/Less' was being written using jquery text overwrote the button (a p element) so it was only there up until the user did the first click.
Keeping the structure of HTML but replacing the onclick function with the code below seems to work. This is deliberately written in naked JavaScript so as not to introduce errors such as the use of text in the original.
var previousClicked='';
$('body').on('click', '.shownow', function() {
if (previousClicked!=this) {
hideTextEl(previousClicked);
}
previousClicked=this;
if (findTextEl.classList.contains('notshow')) {showTextEl(this);
} else {hideTextEl(this);}
function hideTextEl(el){
if (el) {
el.firstElementChild.innerHTML='Read More';
findTextEl(el).classList.add('notshow');
}
}
function showTextEl(el){
if (el) {
el.firstElementChild.innerHTML='Read Less';
findTextEl(el).classList.remove('notshow');
}
}
function findTextEl(el) {
while (!el.classList.contains('main')) {el=el.parentElement;}
return el.firstElementChild;//NB if the structure of the HTML changes this may need changing
}
});
Helo,
I'm working with external software which is generating reports.
I'm getting table, then it is printed to website in div.
I don't have access to this table before it is generated, so i can't set any attribute before website is rendered.
So I need to add attribute to this table as last step of rendering process, it doesn't matter is it ID or Class.
Structure is like:
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
I'm using IE v11.
I tried something like this (nothing happens):
document.getElementById("Checklist").childNodes[0].className = "TestClassName";
Also (it gives mi error: Object doesn't support property or method 'setAttribute' )
document.getElementById('news').childNodes[0].setAttribute( 'class', new_class );
Any other ideas?
If you use ChildNodes it will return all the white space with nodelist so use children so that it will return only child elements
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
Change Your js to
document.getElementById("Checklist").children[0].className="TestClassName";
document.getElementById('news').children[0].setAttribute( 'class', new_class );
it will work
try this to add class
document.getElementById("Checklist").classList.add("TestClassName");
document.getElementById('Checklist').childNodes[1].setAttribute('class', 'table1');
.TestClassName {
color: red;
}
.table1 {
border: solid 1px;
}
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
Try this
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#Checklist").children('table').attr("class", "class_name");
</script>
change class_name with your class name
Assuming this is your code,
<div class="data" id="Checklist">
<p>Some Text</p>
<div id="WhereTableWillGo">
<table></table>
</div>
<p></p>
</div>
You could do the alterations within a window.onload function,
window.onload = function() {
document.getElementById("Checklist").getElementsByTagName("table")[0].classList.add("NewClass");
// OR
document.getElementById("Checklist").getElementsByTagName("table")[0].setAttribute("class", "TestClassName");
}
Or you could fetch the table asynchronously by doing the following, and do your alterations before inserting the table,
var xhttp = new XMLHttpRequest;
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Do Alterations to Table
document.getElementById("WhereTableWillGo").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "https://website.com");
xhttp.send();
:)
I started making a website few days ago and I'm stuck at this step:
http://jsfiddle.net/r6uhczks/
CSS:
/* This section to be trigger of dropdown process */
.parent_style { background: -webkit-linear-gradient(left,rgba(0,0,0,0.5),transparent); width: 325px; height: 21px; }
.parent_style:hover { background: -webkit-linear-gradient(left,rgba(242,182,0,0.7),transparent); }
.parent_style .textP {}
.parent_style:hover .textP {color: red;}
/* This section to be shown by clicking on ↑ and hidden by clicking for second time */
.child_style { background: rgba(0, 0, 0, 0.5); width: 325px; height; auto; }
.child_style:hover { background: rgba(242,182,0,0.7);}
.child_style .textC {color: white;}
.child_style:hover .textC {color: black;}
As you can see I created two styles for dropdown menu,
.parent_style
defines trigger of show/hide content defined by
.child_style
Simplier I want to make a class="parent_style" to be show/hide trigger of shown/hidden class="child_style"
The problem is that I don't know how to create this action by click with CSS/HTML only, I know only by hover.
HTML:
<div class="parent_style">
<p class="textP">Something</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<div class="parent_style">
<p class="textP">Something else</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<div class="parent_style">
<p class="textP">Something else 2</p>
</div>
<div class="child_style">
<p class="textC">Content</p>
</div>
<div class="child_style">
<p class="textC">Content2</p>
</div>
<div class="child_style">
<p class="textC">Content3</p>
</div>
<br>
EDIT: and also I would like to know how to edit spaces between these elements, best to remove them.
You can add state to CSS and HTML by using checkboxes (persistent) or focus (temporary):
http://jsfiddle.net/rudiedirkx/L40zcjfc/
HTML
<button>hold down here</button>
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
<br />
<br />
<button><label for="cb1">click here</label></button>
<input id="cb1" type="checkbox" />
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
CSS
input,
ul {
display: none;
}
button:active + ul,
input:checked + ul {
display: block;
}
The persistence comes from the checkbox's :checked state. You can toggle the checkbox with a <label> so you don't have to know it's there.
The temporariness comes from the :active state of a button. Maybe you can use a normal link, but that might keep focus and/or active.
If you can use custom html and css, i'm sure you can stick a custom script at the bottom of the page if it doesn't allow you to add to the head (keep in mind this script requires the page to be loaded first).
Because the elements are not paired together in individual divs, the javascript is a bit longer but nothing too complicated. This script I created will scan through all the divs on the page and will allow the onclick function to determine what the next three divs are. This means that the script is custom to your layout of navigation so you'll need to change the javascript with a layout change.
Here is a working jsfiddle: http://jsfiddle.net/sLc4svam/
<script>
var divs = document.getElementsByTagName("div");
var parents = document.getElementsByClassName("parent_style");
for (var i=0; i<parents.length; i++) {
parents[i].onclick = function() { toggleChildren(this); };
}
function toggleChildren(elem) {
for (var i=0; i<divs.length;i++) {
if (divs[i] == elem) {
for (var ii=1; ii<=3; ii++) { // The 3 is how many children it will toggle
if (divs[i+ii].style.display == "none") {
divs[i+ii].style.display = "block";
} else {
divs[i+ii].style.display = "none";
}
}
}
}
}
</script>