find text from multiple div having same class jquery - php

<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>

Related

Takes one of the values from the data loop according to its value

I have my data show with arrays, but I have a problem when I want to click on one of the data that appear all the same, must be different, what is wrong with my script
<?php
foreach($data as $row){
?>
<a href="javascript:void(0)" class="set_module">
<div class="col-lg-4 col-xs-6">
<div class="post-module">
<div class="tmb">
<img src="<?php echo base_url();?>assets/file/image-module/<?php echo $row->image; ?>"/>
</div>
<input type="text" name="id_m[<?php echo $row->id_module;?>]" id="id_m" value="<?php echo $row->id_module;?>"></input>
</div>
</div>
</a>
<?php } ?>
This my jquery
$("a.set_module").on("click", function (event) {
event.preventDefault();
var value = $('#id_m').val();
alert(value);
return false;
});
the example above is I have data A, B, C and I show it by looping but when I click one of the data only data A keep appearing
Try like this
change id to class in this line
<input type="text" name="id_m[<?php echo $row->id_module;?>]" class="id_m" value="<?php echo $row->id_module;?>"></input>
and then in js
$("a.set_module").on("click", function (event) {
event.preventDefault();
var value = $(this).find('.id_m').val();
alert(value);
return false;
});
fidle example
http://jsfiddle.net/Q8KVC/3543/
use css class insted of id. and find that class with $(this).
or use dynamic id like id="id_m<?php echo $row->id_module;?>" and change this <a href="javascript:void(0)" class="set_module"> with <a href="javascript:void(0)" class="set_module" data-uid="<?php echo $row->id_module;?>">
. after that change the script
as follows
$("a.set_module").on("click", function (event) { event.preventDefault(); var uid = $(this).data('uid'); var value = $('#id_m'+uid).val(); alert(value); return false;});

Image does not change on mouse over using jquery function

<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");
});

Jquery attribute equals selector only selects the first element

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.

Cant get the value in div class="rateit" readonly mode after ajax call

I am using Jquery RateIt plugin with the view more button.
so In my project i am showing restaurant rating with these plugin by default i am displaying first 5 rating.Then after 5 records i am displaying other content through ajax so in the content loded through ajax rating is not being showd i am using readonly mode..If anyone can help thanks in advance
foreach($list_review as $row): ?>
<div class="user_reviews_data">
<div class="width-20 float-left">
<span class="padleft-10"><?php echo $row->User; ?> </span>
<br/>
<span class="padleft-10">
**<div class="rateit" data-rateit-value="<?php echo $row->Rating;?>" data-rateit-ispreset="true" data-rateit-readonly="true"></div>**
</span>
<div class="muted padleft-10 float-left"><small><?php echo date('dS M Y' ,strtotime($row->CreatedDate)); ?></small></div>
</div>
<div class="width-80 float-left"><?php echo $row->Feedback;?></div>
<span class="report_span">Report this Feedback <img src="<?php echo base_url();?>themes/images/FLAG_GREY.png"></span>
</div>
<?php
$msg_id=$row->Id;
endforeach;?>
</div>
<div id="more<?php echo $msg_id; ?>" class="btn-container center_text morebox">
View More
</div>
Now code of jquery
/*view More Button*/
$('.more').live("click",function()
{
var this_tag = $(this);
var ID = $(this).attr("id");
if(ID)
{
$.post(siteUrl+"ajax/ajax_more",{lastmsg:ID,restid:$(this_tag).data("restid")},function(html){
$("ol#updates").append(html);
$("#more"+ID).remove();// removing old more button
});
}
else
{
$(".morebox").html('The End');// no results
}
return false;
});
I am adding content on view more button click through ajax ajax code is same as above code
I am Using this plugin http://www.radioactivethinking.com/rateit/example/example.htm
In the $.post success handler you should invoke rateit again via JavaScript after you append the HTML like so:
$("ol#updates").append(html);
$(".rateit").rateit();
Replace $(this_tag) with either this_tag or $(this) in your ajax call.
You should use var this_tag = this; instead of using var this_tag = $(this);
OR try var $(this_tag) = $(this);

append html outside of the parent tag using jquery

So I have this scenario
<div id="editor" contenteditable="true">
<div id="list">Hello</div>
</div>
I want to call
var html = $('<div id="replaced" contenteditable="false">Hello</div>');
$('#list').replaceWith(html);
The problem is I want the output to be like this:
<div id="editor" contenteditable="true">
<div id="replaced" contenteditable="false">Hello</div><br />
</div>
I tried the following and it didn't work: (Where this is the div replacing tmp.mention)
$('#'+tmp.mention).replaceWith(this).parent().after('<br />');
I want to do this because the div replacing tmp.mention has contenteditable set to false inside a div editor that has a contenteditable set to true. If there's no the user will not be able to type after the div...
Try this:
$('#'+tmp.mention).replaceWith($(this).parent()).after('<br />');
I think you need
var html = $('<div id="replaced" contenteditable=false>Hello</div>');
$('#list').replaceWith(html);
DEMO
But if you want to whole #editor then
var html = $('<div id="replaced" contenteditable=false>Hello</div>');
$('#list').parent().replaceWith(html);
DEMO
According to edit
If you want output like
<div id="editor" contenteditable="true">
<div id="replaced" contenteditable="false">Hello</div><br />
</div
then try
var html = $('<div id="replaced" contenteditable=false>Hello</div>');
$('#list').replaceWith(html).after('</br>');
try this:
$('#list').replaceWith('<div id="replaced" contenteditable="false">Hello</div><br />')

Categories