How can i add the class selectors dynamically to the img tag. For Example:
img tag should be inside the anchor tag then only the class name:sample should add dynamically whichever anchor tag contain img like,
Before:
<img src="image.png"/>
After:
<img src="image.png" class="sample"/>
If already image tag contain class then remove that class and the new class is possible in jquery. I am not sure, how can i do this in jQuery.
Any suggestion would be great.
Thanks,
vicky
You just need to toggle your class. To do this, see:
$("img").toggleClass("border");
I made an example for you on CodePen. Just click here.
$(document).ready(function(){
$('your selector').addClass('sample'); //It can be select,img,a,etc.. });
Be specific in your tag for choosing. I use in CakePHP 3.x Date HtmlHelper field and it worked like a magic.
if($('a[href=image.png] > img').hasClass('sample')){
$('a[href=image.png] > img').removeClass('sample');
}
And addClass for else part.
This can be done easily via jQuery.addClass and jQuery.removeClass APIs..
Add Class - http://api.jquery.com/addClass/
Remove Class - http://api.jquery.com/removeClass/
This should do it:
$('a img').removeClass().addClass('example')
$(document).ready(function(){
$('a >img').addClass('sample'); //direct descendant of a
});
HTML code:
<img id="testImg" src="xyz.jpg" />
css code:
.displayNone{
display:none;
}
jquery - to add class
`$("#testImg").addClass("displayNone");`
jquery - to remove class
$("#testImg").removeClass("displayNone");
Related
I want to place some text as link inside a div.
For example, I want to place text 'Google' with hyperlink <a href="http://www.google.com" inside a div having class-id as "my-link".
How can this be done using jquery or javascript?
Class and ID is not the same.
If ID try this:
$('#my-link').html('Google');
Demo with ID
If Class try this:
$('.my-link').html('Google');
^
Demo with class
You can do this :
$('.my-link').html('Google');
but it would add hyperlink to all .my-link divs, so it's better to add an ID to div and use the ID on jQuery code.
If my-link is the class of the target div then
$('.my-link').html(function(idx, html){
return html.replace(/Google/g, 'Google')
})
Demo: Fiddle
I have a particular div element with "testDiv" class;
<div class="testDiv">Content</div>
Now using jQuery I want to remove the testDiv class and add a different class;
jQuery('div').removeClass('testDiv');
Now how can I add a class to this particular div that got its class remove, i fear that if I do jQuery('div').addClass('newClass'); it will add a class to all the other divs present in the page.
how about:
jQuery('div.testDiv').addClass('newClass').removeClass('testDiv');
jQuery('div.testDiv').toggleClass("testDiv newClass")
I have the div which is generated when page loads as below.
<div class="redactor_ redactor_editor"></div>
How can I add a class .example onto it via jquery like below?
<div class="redactor_ redactor_editor example"></div>
Thank you very much!
Try addClass this,
$('.redactor_').addClass('example');
just select element using any selector
$(selector).addClass('example');
demo
you can add class to element by using the .addClass()
syntex
.addClass( className )
className One or more class names to be added to the class attribute of each matched element.
so now this is you want
$('.redactor_ redactor_editor').addClass('example');
try this :
<pre>
$(window).load(function () {
$('.redactor_ redactor_editor').addClass('example');
});
</pre>
reference :
addClass
You can set the class regardless of what it was by using .attr() like this,
If the html is like this,
<div class="redactor_ redactor_editor"></div>
then the selector will be like the following,
$(document).ready(function() {
$('.redactor_redactor_editor').attr('class', 'redactor_redactor_editor example');
});
Since your div tag's class name has a space after the underscore sign(i.e. redactor_ ) the following way must be applied to select the element.(<div class="redactor_ redactor_editor"></div>)
$(document).ready(function() {
$("div[class='redactor_ redactor_editor']").attr('class', 'redactor_ redactor_editor example');
});
If you are looking for adding a new css class to an element that already has a class then-
DEMO
HTML-
<div class="one">example</div>
CSS-
.one{color:red;}
.two{color:green}
jQuery-
$(function(){
$('.one').addClass('two');
});
If you are looking for removing the old css class and add a new class-
DEMO
jQuery-
$(function() {
$('.one').removeClass('one').addClass('two');
});
I have a click event and I want to add a class to the current element clicked plus an other element and I want to do that in one line.
I have tried something like
JS file
$('div').click(function(){
$(this, 'span').css('background','yellow');
// could usethe following way but it's not DRY
// $(this).css('background','yellow');
// $('span').css('background','yellow');
});
HTML file
<div>
div
</div>
<span>
span
</span>
Here is a fiddle for exemple
But it doesn't works so how can I do without repeating the css method.
You can use .add(selector) method like so:
$(this).add('span').css('background','yellow');
You can add the <span> elements to the $(this) jQuery instance:
$(this).add( $('span') ).css('background', 'yellow');
or even:
$(this).add('span').css('background', 'yellow');
http://jsfiddle.net/Hhqc8/7/
Try siblings():
$('div').click(function(){
$(this).siblings('span').css('background','yellow');
})
Example:
http://jsfiddle.net/Hhqc8/6/
I am adding an element on a certain condition. But I don't want it suddenly appear but rather would like to slide it down. Here's how I add it:
$('.myDiv').after('<div>added content</div>');
How do I combine it with slideDown?
Try this instead :
$(".myDiv").after("<div style='display:none;'>added content</div>");
$(".myDiv").next("div").slideDown();
Good Luck !!
Try this out:
$('.myDiv').after('<div style="display:none">added content</div>').next().slideDown();
You need to use .next() on the div to which it is attached because .after()... adds the element as a sibling to the div to which it is added
Try this
$('.myDiv').after('<div style="display:none;" class="newDiv">New Content</div>');
$('.myDiv').next('.newDiv').slideDown();
first make sure the new content that you input is hidden using .newdiv{display:none;} either in your css file or inline code like: <div style="display:none;">
then use a callback function to only start after the code was inserted in the document when you used the after() method.
$('.myDiv').after('<div class="newdiv">added content</div>', function(){
$('.newdiv').slideDown();
});