How to use CSS or jQuery to style my background-color in DIV when the id inclue strings in php?
My code is:
<div id='result_'.$item_id.'>item</div>
You can attach a class to your div and use a jquery selector to get your div(by id or class) and change its css properties using .css(). Also I think you want to change the background after clicking on a specific div, so you need to attach also a click handler, extract its id and then change its background color.
<div class="my_class" id='result_'.$item_id.'>item</div>
$(".my_class").click(function(event){
var clicked_id_item = $(event.target).attr("id").split("_").pop();
$("#result_"+clicked_id_item).css('background-color','black')
})
If you can just add a class to your div, you can style it with css:
<div class="my-class" id='result_'.$item_id.'>item</div>
css:
.my-class {
background-color: red;
}
Or if you want to wrap your divs with a .wrapper, your css could be something like:
.wrapper > div {
background-color: red;
}
edit
I can see from your other comments that you are asking about the background-color changing when you click on the div. Although that was not made clear in your question, you could do that like this: (Building on my previous example)
.my-class.active {
background-color: blue;
}
Add some jquery:
$(".my-class").click(function(){
$(this).siblings(".active").removeClass("active"); // Maybe you could use .end() to keep the chain going
$(this).addClass("active");
});
You can use the attribute selector on the attribute id !
[id] matches every element with attribute "id".
[id=blah] is an equivalent to the selector #blah (every element with id="blah").
And [id^=blah] matches every element whose "id" attribute begins with blah.
[id^=result_] {
background: hotpink;
color: #fff;
}
div { margin: .2em; padding: .2em; }
<div id="result_blablah">#result_blablah</div>
<div id="result_hello">#result_hello</div>
<div id="nanana">#nanana</div>
<div class="plop">.plop</div>
Related
I am trying to modify the css of a SVG image(the logo) in a header.php of a Wordpress custom theme.
Here is the code in header.php:
<!-- logo -->
<a class="logo" href="<?php echo home_url(); /* insert home link */ ?>">
<img src="<?php echo get_stylesheet_directory_uri(); /* add theme path */ ?>/img/logo.svg" alt="<?php bloginfo('title'); /* insert blog title */ ?> ">
</a>
To modify the CSS of the SVG I have to inline it, but I don't know how to do, this SVG is saved from Illustrator.
I tried to add this jQuery script that transforms a SVG image in an inline SVG, but I don't the see the image in the browser, I see it if I inspect the element.
/**
* Replace all SVG images with inline SVG
*/
jQuery('img.svg').each(function(){
var $img = jQuery(this);
var imgID = $img.attr('id');
var imgClass = $img.attr('class');
var imgURL = $img.attr('src');
jQuery.get(imgURL, function(data) {
// Get the SVG tag, ignore the rest
var $svg = jQuery(data).find('svg');
// Add replaced image's ID to the new SVG
if(typeof imgID !== 'undefined') {
$svg = $svg.attr('id', imgID);
}
// Add replaced image's classes to the new SVG
if(typeof imgClass !== 'undefined') {
$svg = $svg.attr('class', imgClass+' replaced-svg');
}
// Remove any invalid XML tags as per http://validator.w3.org
$svg = $svg.removeAttr('xmlns:a');
// Replace image with new SVG
$img.replaceWith($svg);
}, 'xml');
});
Thanks for all the support.
Carlo
There are ways to style svg with attributes like fill, stroke or stroke-width for example. This is very usefull if you create you image actualy by code, for example:
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
Sorry, your browser does not support inline SVG.
</svg>
You have an svg image file and use it as src in your <img> tag.
It is not the direct answer to your question, but maybe it solves your problem, if you save the other version of your image also as an svg file (should be less than 10kb, so no performance problems) and change the src value.
In the comment I read you want to achieve a dark / light mode and also your logo image should change if another mode is selected, like:
document.getElementById("my_image_id").src = "my_image_file.svg";
Another solution would be to do all the look changes via the css and just set the data-theme with javascript. CSS will make the rest for you.
You could show your logo as a background-image, using the a parent:
<a class="logo" href="<?php echo home_url(); ?>">
<!-- here comes the logo -->
</a>
and in your CSS:
.logo {
display: inline-block;
width: 50px; /*your dimensions*/
height: 50px;
background-image: url(my_image_file.svg);
background-size: cover;
}
How to create dark / light mode without jQuery and minimum Javascript
This is more than you asked for, but maybe can help your task make more easy. You can give your html element an attribute, for example called data-theme and your button only changes this attribute value with javascript.
So in the html: You change the data-theme value with your toggle button (with a nice transition).
<html lang="en" data-theme="light">
<body>
.
.
.
<input type="checkbox" id="switch" name="theme" /><label for="switch">Toggle</label>
.
.
.
<script>
var checkbox = document.querySelector('input[name=theme]');
checkbox.addEventListener('change', function() {
if(this.checked) {
trans()
document.documentElement.setAttribute('data-theme', 'dark');
} else {
trans()
document.documentElement.setAttribute('data-theme', 'light');
}
})
let trans = () => {
document.documentElement.classList.add('transition');
window.setTimeout(() => {
document.documentElement.classList.remove('transition');
}, 1000)
}
</script>
</body>
</html>
That's all for the javascript and html part of the job. The rest is CSS with normal variables, you set once and use in your code.
html {
--bg: #AEAEAE;
--color-headings: #666;
--logo-source: url(my_light_logo.svg);
}
html[data-theme='dark'] {
--bg: #333333;
--color-headings: #FFF;
--logo-source: url(my_dark_logo.svg);
}
body {
background-color: var(--bg);
}
h1 {
color: var(--color-headings);
}
.logo {
background: var(--logo-source);
}
html.transition,
html.transition *,
html.transition *:before,
html.transition *:after {
transition: all 750ms !important;
transition-delay: 0 !important;
}
This way you change everything about the styling in your css, and only trigger the data-theme change with javascript on your button. For me, this seems to be the more logic way to achive light / dark mode, as CSS is there just for styling.
Hope it helps and maybe is something of interest for you!
.main-header {
background: url(logo.svg) no-repeat top left;
background-size: contain;
}
.no-svg .main-header {
background-image: url(logo.png);
}
something like this.. with the help of CSS.
In jquery, I need to dynamically display first two values and after while click remaining values should be display?
<div class="category_items">
<div class="category_item">
<div class="test">test1_1</div>
<div class="test">test1_2</div>
<div class="test">test1_3</div>
<div class="test">test1_4</div>
<div class="test">test1_5</div>
<div class="test">test1_6</div>
</div>
<button>Show / Hide</button>
<script type="text/javascript" src="//code.jquery.com/jquery-latest.js"></script>
<script>
var elems = $('.category_item', '.category_items').filter(function() {
return $(this).children().length > 2;
}).hide();
$('button').on('click', function() {
elems.toggle();
});
</script>
I want to display first two items by default when i click the button i want show full list
Do this with css with
HTML
<style>
li div{display:none;}
li:hover div {display:block;position:absolute;left:200px;width:100px;height:100px;background-color:red;}
</style>
<ul>
<li>123<div>1</div></li>
<li>222<div>2</div></li>
<li>333<div>3</div></li>
</ul>
enter code here
:gt selector will be good for this situation. I hope, this code helps you.
<script>
$('.category_item .test:gt(1)').hide();
$('button').on('click', function() {
$('.category_item .test:gt(1)').toggle();
});
</script>
If you want to change the number of elements which you want to show, just change number in gt() selector.
See this also but I not sure if is normal to use span as child of ul not li but you can put in last li another ul and when show this new ul.
This all li is show when zou hover ul bu zou can do to show all li onli when hove some think one as example + hover last show li(user no need to click to see al!).
<style>
ul span{display:none;}
ul:hover span {display:block;/*position:absolute;left:200px;background-color:red;*/}
</style>
<ul>
<li>123<div>1</div></li>
<li>22<div>2</div></li>
<span>
<li>223<div>3</div></li>
<li>223<div>3</div></li>
<li>223<div>3</div></li>
</span>
</ul>
I am loading divs with information from the database using php code:
for ($i=0; $i < $stmt->rowCount(); $i++){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$name = $row['uc_name'];
$image_url = $row['uc_image'];
$color = $row['uc_color'];
The creates a box for however many rows there are: http://jsfiddle.net/pKR5t/1/
I am trying to create a drop down menu for each item retrieved from the database. When the user hovers over class="header", the drop down menu should show but the list inside should be pertaining to that of the box hovered:
I believe I can use the .on() function to accomplish this but I am completely lost on how to use it. Any help on getting this accomplished would be helpful. Thanks
To use on and hover you will want to use the mouseenter and mouseleave events like so:
$("div.box div.header").on("mouseenter", function() { //mouseenter event
var currentBox = $(this).closest("div.box");
var currentTitleText = $(this).find("div.title").html();
//popup the list
}).on("mouseleave", function() { //mouseleave event
//close the list
});
For some additional info, check this post.
EDIT
Updated mouseover and mouseout to the slightly better mouseenter and mouseleave events.
You can do this without javascript using css.
If you insert your hover box inside your header div you can use css :hover to hide and show it on hover.
HTML
<div class="header">
<div class="title">PHP Row 1</div>
<div class="hoverBox">
<div class="hoverBoxTitle">Hover</div>
<div class="hoverBoxContent">Hover Content</div>
</div>
</div>
CSS
.hoverBox {
display: none;
position: absolute;
top: 0px;
left: 170px;
width:170px;
height:90px;
background:#333333;
z-index: 100;
}
.header:hover .hoverBox {
display: block;
}
Demo
I'm looking to setup a page which holds an image of grid paper. These grids can be selected and depending on what type of link the user chooses the color will be filled and saved. Once all of the blocks are filled this will be archived and then a new sheet will be displayed. I wanted to use PHP / MySQL on the backend but was wondering what would be good client side?
Why a image for the grid? Better solution is to use a table or a list filled with divs to create the grid. Then use jQuery to fill in the backgrounds when the user clicks a link.
Example for the grid:
<ul id="grid">
<li>
<div class="cell" id="cell_id"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</li>
<li>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</li>
...
</ul>
CSS example:
<style>
#grid {
list-style: none;
}
#grid li {
height: 50px;
}
#grid .cell {
float: left;
width: 100px;
height: 50px;
border: 1px solid black;
}
</style>
And finally use jQuery to fill the cells when a user clicks a link. You can find the cell by calculating the rows and cells or just give the cells a id. The saving part can be done by a AJAX call to a page that will save the information to the DB.
Simple jQuery example (you have to think up the rest yourself):
<script>
$('link').click(function() {
//place check for new sheet here
var id = $(this).attr('id');
//set the bg color
$('cell_id').css('background-color', 'red'); //or color code #FF0000
//save the info
$.post('save_info.php', { cell_id: id, color: "red" }, function(data) {
alert('saved!');
});
});
</script>
You can find more info and download jQuery at: http://jquery.com/
The jQuery code to create a new sheet when all the cells are filled can be done with a little check in the click function. This check must count all the cells that have a bg color, is this the same as the total cells? Then create a new sheet by removing all the cells their bg color and by setting new id's. But you can figure this out by yourself ;).
I want to have an image with it's corresponding block of text rotating every few seconds on my website. Just like these guys http://hellofisher.com/
I know I can get a javascript to rotate the images but I haven't found where I can have the block of text alongside rotating to suit the images.
Just google for jquery content rotators or content sliders. There's a million ready-made plugins already done like this:
For example
I'll show you a simple example which you can easily use on your site. This example uses 3 slides and isn't a dynamic slider where you can have as many slides as you want to but it's easy to edit it into one if the amount of slides can vary.
HTML:
<div id="slider">
<div id="slider-area">
<div class="slide">content</div>
<div class="slide">content</div>
<div class="slide">content</div>
</div>
</div>
Slider is the visible area which the user can see, slider-area is the area where you put the slides and each slide contains a different image and text.
CSS:
#slider {
width: 400px;
height: 400px;
overflow: hidden;
position: relative;
}
#slider-area {
width: 1200px;
height: 400px;
position: absolute;
top: 0;
left: 0;
}
.slide {
width: 400px;
float: left;
}
As you can see, slider-area's width is 3 times the size of slider because there are 3 slides. Slider has overflow: hidden which means scrollbar won't be visible and position: relative so that we can absolutely position the slider-area inside the slider. Then we simply insert slides inside the slider-area.
JS (using JQuery):
$(document).ready(function() {
var timer = setInterval(changeSlide, 5000);
});
function changeSlide() {
var pos = $('#slider').attr('scrollLeft');
if (pos==800)
pos=0
else
pos=pos+400;
$('#slider').animate({scrollLeft: pos}, 600, 'swing');
}
Here you set a timer which will call the changeSlide function every 5 seconds. The changeSlide function will take the current position of the slider's scrollbar and animate it accordingly. If it's displaying the last slide, next one will be the first slide (pos=0) and if it's not displaying the last slide, it adds 400 to the position so that it will scroll to the next slide. Basically the sliding animation is simply moving a hidden scrollbar with a timer.
Hopefully that helps!
Just use jQuery to rotate out a <div> containing an image and associated text. Use $.hide() and $.show() and Javascript's time functions.
You could try:
JAVASCRIPT
var imagelist = ["IMAGE1","IMAGE2","IMAGE3"]
var textlist = ["TEXT1","TEXT2","TEXT3"];
counter=0;
countmax=3;
var next = function(){
$('rotating_image').innerHTML="<img src=\"" imagelist[counter%countmax] + "\">";
$('rotating_text').innerHTML=textlist[counter%countmax];
countmax++;
}
var rotate = window.setInterval(next, SECONDS * 1000);
HTML
<span id="rotating_image"></span>
<span id="rotating_text"></span>