<a href="#">
<div class="col-md-5 campaigns" id="hoverimg">
<img class="featurette-image img-responsive" src="images/tee1.png" >
<li class="progressbar">
<p>
<!--<strong>Orders Completed</strong>--> <span class="pull-right small muted">78%</span>
</p>
<div class="progress tight">
<div class="bar" style="width: 78%;">
</div>
</div>
</li>
<div class="prodheading">
<h2>Heading 1</h2>
</div>
</div>
</a>
jQuery code:
<script>
$('document').ready(function ()
{
$(function ()
{
$("#hoverimg")
.mouseover(function ()
{
var src = $(this).attr("src").match(/[^\.]+/) + "images/tee2.png";
$(this).attr("src", src);
})
.mouseout(function ()
{
var src = $(this).attr("src").replace("images/tee2.png");
$(this).attr("src", src);
});
});
});
</script>
I am not getting the real problem behind this.
Your JavaScript is completely commented out by having // in front of every line. These will have to be removed if you want anything to happen.
There are some issues:
.replace() is a JavaScript String method and requires two arguments:
the regular expression and
the replacement string.
.match(/[^\.]+/) + "images/tee2.png probably does not work as you expect it to do: .match(/[^\.]+/) will return the first part of the string it is applied on that does not cointain a dot ('.'). Is this really what you want to do?
example:
"path/subpath/filename.ext".match(/[^\.]+/) + "images/tee2.png"
will return
"path/subpath/filenameimages/tee2.png"
You have applied the id to the div tag and in jquery you are trying to get the src. You must assign the id to the img tag.
$("#hoverimg1").hover(
function() {$(this).attr("src","images/teee2.png");},
function() {$(this).attr("src","images/teee1.png");
});
Related
<div class="textbox-fill-mid">
<div style="display:none;" class="bbit-cs-id">171</div>
<div style="cursor:pointer;" >adad</div>
</div>
<div class="textbox-fill-mid">
<div style="display:none;" class="bbit-cs-id">11</div>
<div style="cursor:pointer;" >adad</div>
</div>
<div class="textbox-fill-mid">
<div style="display:none;" class="bbit-cs-id">41</div>
<div style="cursor:pointer;" >adad</div>
</div>
my url : localhost/pr1/ev.php?eid=41
if(isset($_GET['eid']))
{
echo "<div id='notifi_event_id'>".$_GET['eid']."</div>";
}
using jquery
var notifi_event_id=$('#notifi_event_id').text(); // having 41
I know that all elements with different values are located into .bbit-cs-id, but I don't know how to find any of the .bbit-cs-id having 41 or not ,
Hope this also will help you
var is41=false;
$('.bbit-cs-id').each(function(i,data){
if($(this).text()=='41')
is41=true;
});
alert(is41);
http://jsfiddle.net/ilaiyaraja/AE9zy/1/
To find an element containing a specific text value, you need to use filter():
var notifi_event_id = $('#notifi_event_id').text(); // = 41
var $bbit = $('.bbit-cs-id').filter(function() {
return $(this).text() == notifi_event_id;
});
// you can then do something with the matching element:
$bbit.addClass('foo');
Example fiddle
You can use this syntax to access each div :
var cases = $('div.bbit-cs-id').each(function()
{
// You access each object with : $(this)
// So you can access Text with the following line :
console.log($(this).html());
});
use jQuery :contains operator to look for text content:
if($('.bbit-cs-id:contains(41)').length){
console.log($('.bbit-cs-id:contains(41):first')); //print the first element.
//update the parent or the sublink.
$('.bbit-cs-id:contains(41):first').next().css({color:'red'});
$('.bbit-cs-id:contains(41):first').parent().css({border:'1px solid gray'});
}
Fiddle
Note:
it if this div is just to hold the ID of something ... why not use data attribute to the container?:
<div class="textbox-fill-mid" data-id="41">
<div style="cursor:pointer;" >adad</div>
</div>
<script>
$('.textbox-fill-mid[data-id=41]');
</script>
My equals selector is only selecting the first item on my list.
It doesn't matter which image I click, it always sets my jQuery variables to the first item.
Thanks!
//PHP
<div class="imgContain">
<img id="finalimg" alt="first" src="website.com/imga546.jpg">
<img id="finalimg" alt="second" src="website.com/imga645.jpg">
<img id="finalimg" alt="thrid" src="website.com/imga6786.jpg">
<img id="finalimg" alt="4th" src="website.com/imga31234.jpg">
</div>
//jQuery
$('.imgContain').on("click", "img", function () {
var srcc = $('img[id="finalimg"]').attr('src');
var linkU = $('img[id="finalimg"]').attr('alt');
});
Since id must be unique, you need to use class instead:
<div class="imgContain">
<img class="finalimg" alt="first" src="website.com/imga546.jpg">
<img class="finalimg" alt="second" src="website.com/imga645.jpg">
<img class="finalimg" alt="thrid" src="website.com/imga6786.jpg">
<img class="finalimg" alt="4th" src="website.com/imga31234.jpg">
</div>
then you can use . to target elements by class as well as using $(this) to get reference to current clicked image with class finalimg:
$('.imgContain').on("click", ".finalimg", function () {
var srcc = $(this).attr('src');
var linkU = $(this).attr('alt');
});
Fiddle Demo
You get Unique ID of image on its click and do manipulation as follows
Working fiddle demo http://fiddle.jshell.net/8kmdt/
<div class="imgContain">
<img id="img1" alt="first" src="website.com/imga546.jpg">
<img id="img2" alt="second" src="website.com/imga645.jpg">
<img id="img3" alt="thrid" src="website.com/imga6786.jpg">
<img id="img4" alt="4th" src="website.com/imga31234.jpg">
</div>
then change your jQuery code to:
$('.imgContain').on("click", "img", function () {
var getID=$(this).attr("id");
var source = $("#"+getID).attr('src');
var linkOfImg = $("#"+getID).attr('alt');
});
element id's need to be unique. you should use classes instead.
i am creating a comment system using the php and mysql to store data and ajax with jquery all the system work well but when it comes to the delete action nothing it happen like this button do not have any action or relation with the system can anyone help me ???
comment_box.php
<li class="comment-holder" id="_<?php echo $comment->comment_id; ?>">
<div class="user-img">
<img src="<?php echo $user->profile_img; ?>" class="user-img-pic" />
</div>
<div class="comment-body">
<h3 class="username-field">
<?php echo $user->userName; ?>
</h3>
<div class="comment-text">
<?php echo $comment->comment; ?>
</div>
</div>
<div class="comment-buttons-holder">
<ul>
<li id="<?php echo $comment->comment_id; ?>"class="delete-btn">X</li>
</ul>
</div>
</li>
comment_delete.js(i am testing the delete button with firebug)
$(document).ready(function() {
$('.delete-btn').each(function() {
var btn = this;
$(btn).click(function(){
console.log("the id " + btn.id);
})
});
});
Try this
$(document).ready(function() {
$('.delete-btn').each(function(i,el) {
$(el).click(function(){
console.log("the id " + $(this).attr('id'));
})
});
});
<li id="<?php echo $comment->comment_id; ?>"class="delete-btn">X</li>
Since you are missing whitespace here between the id value and the class attribute, I think the browser just does not assign that class to the element.
Edit (for people who don’t know what “missing” means):
Your PHP code will generate HTML output of the form
<li id="123"class="delete-btn">X</li>
whereas it should of course be
<li id="123" class="delete-btn">X</li>
Please always validate your HTML code before asking questions.
Try
$(document).ready(function() {
$('.delete-btn').click(function() {
var comment_id = $(this).attr('id');
alert(comment_id);
});
});
I'm trying to slide one div closed, and another open at the same time using a button. here is my div code:
<div class='toggle_parent'>
<div class='toggleHolder'>
<p>Content here</p>
<span class='toggler'><button class="toggler" type="submit">Send Error Report</button></span>
</div>
<div class='toggled_content' style='display:none;'>
<p>Hidden Content</p>
</div>
</div>
And then the jQuery is as follows:
$(document).ready(function(){
$('.toggler').click(function() {
$('.toggleHolder').slideUp().css('display', 'none')
$('.toggled_conent').slideDown().css('display', '')
});
});
I've tried so much that my brain doesn't function anymore and I know the answer will be "so easy I should have figured it out", but I'm stuck. I want the toggled content to appear and the toggleHolder to go away when you press the toggler.
Thanks in advance!
Hide your hidden content via javascript instead of the style, and get rid of the .css calls:
$(document).ready(function(){
$('.toggled_content').hide();
$('.toggler').click(function() {
$('.toggleHolder').slideUp();
$('.toggled_content').slideDown();
});
});
Also you had a typo in your .toggled_content selector.
Here's a working JSFiddle.
Have you tried this?
$('.toggler').click(function() {
$('.toggleHolder').slideUp().hide();
$('.toggled_conent').slideDown().show();
});
Please change class name = "toggled_content" to something else in div and jquery so this looks like
jQuery
<script type="text/javascript">
$(document).ready(function(){
$('.toggler').click(function() {
$('.toggleHolder').slideUp().css('display', 'none');
$('.haveOther').slideDown();
});
});
</script>
HTML
<div class='toggle_parent' style="background-color:#999;">
<div class='toggleHolder'>
<p>Content here</p>
<span class='toggler'><button class="toggler" type="submit">Send Error Report</button></span>
</div>
<div class='haveOther' style='display:none;'>
<p>Hidden Content</p>
</div>
</div>
This will work for you.
I have the following script which appears multiple times on my page. I have a simple slide toggle attached to <div class="info"> and contolled by <a rel="viewinfo"> tag, but when I click on the anchor, all the divs with class info slide.
here is the HTML/PHP
<div class="couplistMeta">
<a class='view' rel="viewinfo" href="#">View Coupon</a>
<a class="print" href="#">Print</a>
</div>
<div class="info">
<p><?php echo $rec2[4]." -- Phone: ".$rec2['phone']; ?></p><p><?php echo $rec2['address']." -- ".$rec2['city'].", ".$rec2['state']." ".$rec2['zip']; ?></p>
</div>
Here is the Javascript
$(document).ready(function() {
$('div.info').hide();
$("a[rel='viewinfo']").click(function() {
$('div.info').slideToggle(400);
return false;
});
});
I tried using $(this).next('div.info').slideToggle(400); but this just broke the effect. So $('div.info') is too general, how can I be more specific with the JS?
Try this:
$(document).ready(function() {
$('div.info').hide();
$("a[rel='viewinfo']").click(function() {
$(this).parent().next('.info').slideToggle(400);
return false;
});
});
The issue being due to $('div.info') implicitly iterating over all div elements with the class info on the page.