How to find the div have the particular class - javascript

html:
<tr>
<td class="last_msg" id="408"><a href="/index/408" id="408">
<div id="div408" class="msg-div">
<div class="msg-actions">
<div id="unr_msg408" class="msg-unread"></div>
</div>
<div style="margin-top:14px; margin-bottom: 14px; line-height: 21px;">
<span class="msg-sender-name">Name</span>
<span class="msg-date">Date</span><br>
</div>
</div>
</a>
<span class="msg_dlt_icon"><img id="dlt408" src="/public/images/icon-delete-blue.png" class="msg_icon_dlt" onclick="return deleteMessage()"></span>
</td>
</tr>
On clicking the span with class msg_dlt_icon,i need to check whether the div with class msg-actions have the div <div id="unr_msg408" class="msg-unread"></div>.
I tried the below line,
var unread =$(this).closest("tr").find('.msg-actions').hasClass('.msg-unread');
I alerted the unread variable,it always return false.
I want to find whether <div id="unr_msg408" class="msg-unread"></div> is present inside <div class="msg-actions"> .
Is it possible to do.

So you need to first find the children of .msg-actions, then check whether it has the class msg-unread.
var unread =$(this).closest("tr").find('.msg-actions > div ').hasClass('msg-unread');

you could use
var unread = $(this).closest('tr').find('.msg-actions .msg-unread').length;

This
.hasClass('.msg-unread')
is incorrect. hasClass already implies that you're looking for a CSS class. You're trying to treat it as a call like $('.msg-unread'). Your hasClass() is telling jquery to literally looking for an element that looks like
<foo class=".msg-unread">
^---note this .
The call should be just
.hasClass('msg-unread')
WITHOUT the ..

Related

if data attribute is true, add class

I want buttons in a slider to get underlined when a slide is visible.
I think I need to check if a data attribute is true, and then add class.
When inspecting my webpage, I find this in properties > dataset: DOMStringMap > isactiveslide: "true"
I need to check if a slide has isactiveslide: "true" (or even data-isactiveslide: "true") and then add class.
I think I am close and have tried these two codes:
jQuery(function () {
if (jQuery('menu1').attr('isactiveslide') === true) {
jQuery(this).find("#test1").addClass("underline");
}
})
and
jQuery('menu1').each(function(){
if(jQuery(this).attr('isactiveslide')==true())
jQuery('#test1').addClass('underline');
})
EDIT (added after some great answers and questions)
And here is the section, where the data attribute "isactiveslide" occurs, copied from the page:
<rs-slide data-key="rs-1898" data-title="WORKS" data-in="o:0;" data-out="a:false;" class="menus works1" id="works1" data-originalindex="2" data-origindex="1" data-description="" data-sba="" data-scroll-based="false" style="overflow: hidden; height: 100%; width: 100%; z-index: 20; opacity: 1; visibility: inherit;" data-owidth="300" data-oheight="200" data-rspausetimeronce="0" data-isactiveslide="true"><
So, the next slide which is not yet shown has data-isactiveslide="false". I reckon, identifying "true" is how I can add class.
EDIT May 4th - I think I am close now, but it still does not work.
jQuery('#slide1[data-isactiveslide="true"]')("#slide1-btn").addClass('.underline');
any help is very appreciated!
Can be easily done by css:
You need to find the class applied on the active slide and button
rs-slide.menus[data-isactiveslide="true"] .button-class-name-here{
text-decoration:underline!important;
}
or
Find which slider you are using and on the slide change event of that slider apply the class on the button for styling.
Try this code:
var $ = document.querySelectorAll.bind(document) //No need for jquery - simply import the function
$(".menu1[data-is-active-slide]").forEach((el, index) => {
$("#test1")[index].classList.add('underline');
$("#test1")[index].innerText = "Selected!";
console.log(1);
})
<div class="menu1" data-is-active-slide='true'>1</div>
<div id="test1"></div>
<div class="menu1" data-is-active-slide='false'>2</div>
<div id="test1"></div>
<div class="menu1">3</div>
<div class="menu2" data-is-active-slide='false'>4</div>
<div class="menu2">5</div>
<div class="menu1" data-is-active-slide>6</div>
<div id="test1"></div>
<div class="menu2">7</div>
<div class="menu1 menu2" data-is-active-slide="true">8</div>
<div id="test1"></div>
<div class="menu1 menu2">9</div>
The beginning declaration of $ is simply defining it since I did not import jQuery.
The next part is where the 'fun' begins. I used $(".menu1[data-is-active-slide]") to select all elements with class menu1 and with the property that data-is-active-slide is present. Then, I simply defined an action inside the function, for the sake of demonstrating that it works.

AngularJS ng-mouseenter not working in child div

I have been using the AngularJS ng-mouseenter and mg-mouseleave and it has been almost the cause of my death. A quick explanation:
<div class="characterSum">
<div class="avatarContainer" ng-mouseenter="showButton = true" ng-mouseout="showButton = false">
<img ng-src="{{imagePath}}" class="img-thumbnail">
<div class="addImage" ng-show="showButton && (imagePath == 'images/chars/defaultCharacterAvatar.png')">
<button class="btn-btn-default">
Add Character Image
</button>
</div>
</div>
</div>
The CSS properties:
.avatarContainer {
max-width: 150px;
max-height: 150px;
display:inline-block;
float:left;
margin-right:5px;
position:relative;
}
.characterSum {
border: 1px;
min-height:160px;
position:relative;
}
I'm fairly certain it has something to do with my CSS properties. I followed a few instructions to automatically scale an image into a 150 x 150 size so that might explain my CSS properties for those wizards. Anyway, the reason why I think it has something to do with it is because when I add this:
<div ng-mouseenter="showButton = true" ng-mouseout"showButton = false">Hi</div>
And I add it under the parent class "characterSum", when I mouse over Hi, the button shows. As soon as I add this under the class "avatarContainer" the child div, it stops working. If I wrap the ENTIRE avatarContainer class with this div so:
<div ng-mouseenter="showButton = true" ng-mouseout"showButton = false">
HI
<div class="avatarContainer"> ......... </div>
</div>
It only shows the button when I go near hi. I added $scope.$watch on showButton to console.log('detected') whenever showButton changes and in every scenario, "detected" is never logged when I go over the img or anything EXCEPT FOR when I go over hi
Does anyone have any ideas on what crazy curse I have been put under? Or if not, I'm willing to use any other way of accomplishing this. (Basically want the button to show whenever the img is moused over). And I have already tried directly applying ng-mouseover to the img element to no avail.
You had a typo, you need to use ng-mouseleaveinstead of ng-mouseout
Markup
<div class="characterSum">
<div class="avatarContainer" ng-mouseenter="showButton = true"
ng-mouseleave="showButton = false">
<img ng-src="{{imagePath}}" class="img-thumbnail">
<div class="addImage" ng-show="showButton && (imagePath == 'images/chars/defaultCharacterAvatar.png')">
<button class="btn-btn-default">
Add Character Image
</button>
</div>
</div>
</div>

find is not working to select data from dom in jquery

what i need
i need to access href from class events_links .
html code
<div class="row flush frt" itemscope="" itemtype="http://schema.org/Event">
<div class="12u">
<div class="evntblk">
<a itemprop="url" class="events_links" href="/dailymail-ideal-homeshow">
<h2 class="lnh1" itemprop="name">Ideal Home Show - London</h2>
</a>
<div style="display:block; float:right; width:auto; color:#7c7c7c;"></div>
<span itemprop="startDate" class="startdates" content="2015-03-20">20 Mar-06 Apr 2015</span><span itemprop="endDate" class="enddates" content="2015-04-06"></span><br><span itemprop="location" itemscope="" itemtype="http://schema.org/Place"><span itemprop="name" style="display:none">Olympia Exhibition Centre</span><span itemprop="address" itemscope="" itemtype="http://schema.org/PostalAddress"><span itemprop="addressLocality" class="eventcity">London</span>,
<span itemprop="addressCountry" class="eventcountry">UK</span></span></span>
<p class="tal" style="overflow:hidden">The ZEE Asian Pavilions are a celebration of British Asian Culture, that encapsulates Asian food, Asian fashion,...<br><span style="background: none repeat scroll 0 0 #ECECEC; border-radius: 5px; color: #333333; display: inline-block; font-size: 0.8em;line-height: 13px; margin: 5px 2px 0 0 !important; padding: 5px 8px;">Home Furnishings & Home Textiles</span></p>
</div>
<div class="row flush footer"><div class="12u"><a class="button button-blue small">View Details</a></div></div>
js code
$('.small').click(function()
{
alert("test");
window.location.href = $(this).find(".events_links").attr("href");
});
snapshot of html element
i have tried to access with .parent() but it not working.
o/p
i need to access events_links class by click on class small so that i
would get href from that html element.
any suggestion are most welcome.
Simple solution to get only the related url is with parent() function:
$('.small').click(function()
{
window.location.href = $(this).parent().parent().parent().find(".events_links").attr("href");
});
since you are three levels underneath.
find() will start searching from the given object downwards in hierarchy.
BUT as stated before, this will fail, as soon as you change your html layout and maybe drop or add a div container.
it would be much better practice to give your divs containing the urls unique id's or store the link with the data attribute of the button.
So for example if in your HTML Code you had this
<div id="link12" class="event_links" href="FOUND THIS!">
<div class="whatever">
<div class="anotherone">
<div class="button small" data-link="link12" data-href="FOUND THIS HERE TOO!">
CLICK HERE
</div>
</div>
</div>
</div>
Then your click code could access URL via 2 methods:
$('.small').click(function()
{
// METHOD 1: get by storing unique id with button
alert($('#'+$(this).attr('data-link')).attr("href"));
// METHOD 2: same effect, but shorter storing href at button
alert($(this).attr('data-href'));
});
try this
$('.small').click(function() {
alert("test");
window.location.href = $(".events_links").attr("href");
});
I would suggest data-attr in this case, where you do not want to depend on code structure and css.
<div class="row flush footer">
<div class="12u">
<a class="button button-blue small" data-relative-path="some-location">View Details</a>
</div>
</div>
And
$('.small').click(function(){
window.location.href = $(this).data('relative-path');
});
You can try this one:
$('.small').click(function() {
alert("test");
window.location.href = $(".evntblk .events_links").attr("href");
});
If you need to get only href of the single .event_links object in the current .box.panel do:
$('.small').click(function()
{
alert("test");
window.location.href = $(this).closest(".box.panel").find(".events_links").attr("href");
});
closest will get you the first parent element that matches the selector. This way if you have any other .event_links that are not into this panel, they'll be skipped. If you use parent() you'll be forced to keep the same HTML sturcture between the .small and it's parents.
First of all it's bad practice to try and load a url based on a css class. That is because it is probable you use css classes repeatedly on a single page, resulting in multiple anchor selections.
However you could do the following:
$('.small').click(function()
{
var btn = $(this);
// find the closest element of click button with class 'event-block'
// find the element with class 'events_links'
// change window location to value at href
window.location.href = btn.closest('.event-block').find('.events_links').attr("href");
});

Angular elements bound to same function. Avoid multiple call on each digest

Given the following:
<div data-ng-repeat="val in vals">
<div data-ng-class:{'myClass':isEngineOn(val)}>
<span data-ng-show="isEngineOn(val)">yeah it's on</span>
<span data-ng-show="!isEngineOn(val)"> standby</span>
<button data-ng-disabled="isEngineOn(val)">Start engine</button>
<div>
</div>
isEngineOn changes with websocket messages I receive from the server.
Is there a way to avoid evaluating isEngineOn, 4 times during each digest, per each value in the repeat? Something like initializing a variable or something?
You can do that with simple CSS and without calling the function in the spans. I think thats much more efficient than a JavaScript solution, especially when you have a repeat over many elements because you can save bindings:
<div data-ng-repeat="val in vals">
<div data-ng-class:{'myClass':isEngineOn(val)}>
<span class="showWhenOn">yeah it's on</span>
<span class="showWhenOff"> standby</span>
<button onclick="alert('Starting...')">Start engine</button>
</div>
</div>
CSS:
.showWhenOn {
display: none;
}
.myClass .showWhenOn {
display: inline;
}
.myClass .showWhenOff {
display: none;
}
.myClass button {
pointer-events: none;
color: silver;
}
JS Fiddle
Yes, just have a variable in the controller that holds the result, and update that variable when you receive an update from the server:
In your controller:
$scope.engineOn = isEngineOn();//also make sure to update from server whenever you need
Note that if your isEngineOn() function is no longer called from the view it doesn't need to be on the scope, it can be declared as a regular variable.
HTML:
<div data-ng-class="{'myClass':engineOn}">
<span data-ng-show="engineOn">yeah it's on</span>
<span data-ng-show="!engineOn"> standby</span>
<div>
EDIT 2: The ng-init, as you said, did not update on digest so I've taken a second approach.
I've taken the long way round, and created a corresponding array for your values and you can read from that:
<div data-ng-repeat="val in vals" >
<div data-ng-class="{'myClass':enginOn[val]}">
<span data-ng-show="engineOn[val]">yeah it's on</span>
<span data-ng-show="!engineOn[val]"> standby</span>
<button data-ng-disabled="isEngineOn(val)">Start engine</button>
</div>
</div>
Fiddle.
You can set watcher to variable that changes when you receive answer from server, for example:
scope.$watch(function(){ return <var that changes>; }, function(oldv, newv){
if(oldv !== newv)
scope.isEngineOn = <Calculate here>;
});
Then use:
<span data-ng-show="isEngineOn">

onclick defined text-var returns emptystring on log

var listname=$(this).parents(".x-grid3-row-table").children(".x-grid3-cell-inner.x-grid3-col-1").text();
console.log(listname);
the console.log returns following:
(an empty string)
can you help me on why listname is empty? and could you also tell me how to pass listname as parameter to a server-side method in an Ext.Ajax.request?
this is the html-code coming from my generated page:
<table class="x-grid3-row-table>
<tr>
<td class="x-grid3-col x-grid3-cell x-grid3-td-1 " tabindex="0" style="text-align: left;width: 265px;">
<div class="x-grid3-cell-inner x-grid3-col-1" unselectable="on">foo_bar#domain.de</div>
</td>
<td class="x-grid3-col x-grid3-cell x-grid3-td-2 x-grid3-cell-last " tabindex="0" style="width: 59px;">
<div class="x-grid3-cell-inner x-grid3-col-2" unselectable="on">
<span class="free">free</span>
</div>
</td>
</tr>
<tr class="x-grid3-row-body">
<!-- 7 Elements Markup down, who all act wrapping for the following -->
<div class="details">
<!-- some other markup elements, who also contain data, but are not further interesting-->
<td class="data"> foo bar detail data </td><!-- not of interest-->
<td class="edit"><input value="edit" onclick="see function on top" type="button" /> </td>
</div>
</tr>
</table>
target is to extract: foo_bar#domain.de and pass it to a server-method as parameter. the method is supposed to make a Window, but that's a different story.
the onclick call is from a expanded grid panel body, which is wrapped in a table('.x-grid3-row-table') with the given html-code.
You have a space between class names in your children selector, however your markup suggests that both the class names are of same elements.
To select an element with more than one class names the selector should not have space between class names as .classname1.className2.
var listname=$(this).parents(".x-grid3-row-table").children(".x-grid3-cell-inner.x-grid3-col-1").text();
console.log(listname);
should work.
Further clarification:
If you have space between class names as in question it would mean that you are trying to select an element having .x-grid3-col-1 class within .x-grid3-cell-inner class. Which is parent child relationship.
Edit:
If your button is in the next row you can use .prev() selector of JQuery with combination of using tr as parent selector rather than table.
listname=$(this).parents("tr").prev().children(".x-grid3-cell-inner.x-grid3-col-1").text();
Or if it is in same row then simply
listname=$(this).parents("tr").children(".x-grid3-cell-inner.x-grid3-col-1").text();
There might be a cleaner way of doing this but I came up with this as your button click handler, it assumes that the text() your looking for is one table row up from the button.
var $rows,button=this,i;
$rows=$(".x-grid3-row-table tr");
for(i=0;i<$rows.length;i++){
// get the button of that row
// not sure how to as your button is not
// in a table row with the code you posted
if($rows[i].getElementsByTagName("input")[0]===
button){
break;
}
}
// I assume the button is in a row before the button
console.log($($rows[i-1])
.find(".x-grid3-cell-inner.x-grid3-col-1")
.text()

Categories

Resources