Learn Java Programming online by Digital Marketing Companies in Delhi
WEBVTT So show an example of you know a situation where you might care about the data attribute we could check if there's kind of a population for example and then we could do styling conditional on that. So what I mean by that is we can say data Daut population equals and you know what again we can just kind of pass it population OK. So now each of these will have a data population field and just as a sanity check I will make sure that is the case population. OK. So we can kind of check this and then style these to kind of highlight the uncarpeted planets just as a simple example. Learn Java Programming online by Digital Marketing Companies in Delhi So let's do something like this for the highlights button ID equals I later show unpopulated planets or highlight on a populated planet. I like. OK. And then we can just kind of do a function here. Function and let's see you know we'll attach an event listener first to that button. So phones later equal document get element by ID. I later dot add event listener quick show on populated you lead to. OK, so function shows unpopulated. So, first of all, we can say all planets Oh planet did equal document dot queries selector or we're looking for all of that Digital Marketing Companies in Delhi have the class. Class of planet Caylus will create a node list which means we can do a loop. So as you can see loops superpowerful stuffier so will say prop of our planet divs. And the way we can access the data set is well you can just grab it because this is going to give us all the did.
That's not going to go anywhere just in the background. And then we can say Propp dot. Class list toggle yellow OK let's see if this works as intended. Boom Boom. OK, it didn't work. And oh I didn't call it yellow. I called it highly. OK. Let's try it again refresh shall multiple highlights. Aha. Now we can again Digital Marketing Companies in Delhi Boom boom boom boom boom. Pretty cool. A little bit of a tangent but I did want to mention that this data actually can be pretty useful. OK so, first of all, we will say value and we can grab the target if we do something like this. We don't target value it so as a sanity check, as usual, your Consolo get to eat at target value let's refresh open a console. Gate change we should get. Unknown. Awesome work. Medium-low. OK. So that's working as intended. We're accessing the target's value of whatever target we are on in our selector, therefore, we can actually snatch that out. Konst value equals event target value but we structured it. Now we can basically write this function appear to be more reusable therefore we can say for Konst of all planet divs which will have been updated by the time we run this highlight function or not and if not just won't do anything. OK so here we can check Propp dot data set that population is equal to the value that we snatch out from the selector well, in that case, we can say Propp thought well the style here style that background equals Teale.
I think that's a pretty start where the color This should be all we need to get it to work. Let's try to get closer. So let's get the planets. Let's try Digital Marketing Companies Delhi. Unknown haha. Success right so with just a couple lines of code by using this data attribute on the population we are able to access that match that against the value of something else and we can really see how we can write concise and reusable code here right. One issue though is that if we switch to say a high population it doesn't remove the style from the other targets. And you know that's very simple to fix. We'll just do for every loop through we'll just remove the stat background color so we can say prompt that style of background equals white for example. So every loop through they'll all get the style set to white and then the ones that match will get them set to Teall so that should cure that slight issue. Now let's get the planets we can say. UNKNOWN OK let's check out the low population success medium. OK, we have a couple of medium three medium ones. And finally the high
MäkerFürnitüre © 2019