How to combine many comparable find query in javascript - javascript

I made this script in javascript, it's work:
if(datatermid==16){
$('#Xhaut').find('[data-parent=16]').removeClass('active')
}
if(datatermid==17){
$('#Xhaut').find('[data-parent=17]').removeClass('active')
}
if(datatermid==18){
$('#Xhaut').find('[data-parent=18]').removeClass('active')
}
I thinks it's possible to combine all this lines into one , maybe somethink like :
$('#Xhaut').find('[data-parent=datatermid]').removeClass('active')
But it dont work...
can you please give me a solution?
Regards

Concatenate the variable using the + operator to build your selector :
$('#Xhaut').find('[data-parent='+datatermid+']').removeClass('active')
You may also do the search in one step :
$('#Xhaut [data-parent='+datatermid+']').removeClass('active')

Related

How to prioritize sum before string conversion in JavaScript?

Is there a way instead to do like this
"4"+4+3 which will be equal with "443" to somehow do first 4+3 like the result to be "47" (as string)? I have tried in many ways, but no one seems to work. Some ideeas? Thanks.
ps. Not switching numbers
Put the values you want to add in brackets and then concatenate
"4"+(4+3)
Use this
console.log("4"+ (4+3))
const num1=4;
const num2=3
console.log("4"+(num1+num2));
you can use the following code

Should we use jQuery custom selector, they are working correct?

I have created simple code
$.expr[':'].test = function(node,index){
console.log(node,index)
}
and executed them
$('div:test()')
All index was 0 and I don't now why?
Should use them or not ?
I don't want use additional libraries
I think its supposed to be $('div:test'). When I tried it in the browser I got a variety of indices. It seems to work

Converting plain JavaScript to jQuery

really simple question, just having a hard time actually making it work. I have a snipet of code that's javascript that I'm trying to write in jquery and can't quite get it.
effects_of_yoga_2010_INFO.style.setProperty('-webkit-transform',
'rotateZ('+effects_of_yoga_2010_DEG+'deg)');
and I had tried it as
$("#effects_of_yoga_2010_INFO").css("-webkit-transform",
"rotateZ('+effects_of_yoga_2010_DEG+'deg)");
but the jquery snippet doesn't work, I'd really appreciate any help I can get on this, I'm sure it'll be breeze for someone.
It should be:
$("#effects_of_yoga_2010_INFO").css("-webkit-transform",
"rotateZ("+effects_of_yoga_2010_DEG+"deg)");
you were mixing single quotes with double quotes.
Assuming effects_of_yoga_2010_DEG is another id of an element with a value, try:
$("#effects_of_yoga_2010_INFO").css("-webkit-transform",
"rotateZ('" + $("#effects_of_yoga_2010_DEG").val() +"'deg)");
$("#effects_of_yoga_2010_INFO").css("-webkit-transform", "rotateZ('+effects_of_yoga_2010_DEG+'deg)");
That doesn't look right. Unless this was a copy-paste error you should split the variable values from the rest of the strings the same way as the JavaScript version had done.
$("#effects_of_yoga_2010_INFO").css("-webkit-transform", "rotateZ(" + effects_of_yoga_2010_DEG + "deg)");
Of course, this is based on not knowing what effects_of_yoga_2010_DEG is. I'm assuming it simply contains the value you are looking for and can be used the same as the JavaScript version had done.

show images using javascript and CSS

I have to create a javascript code which calls different images on the click of an image button. I have given names to the images as product1, product2, product3. I am new to JavaScript.
var count;
document.getElementById("divProduct").style.backgroundImage="url('images/product'+count)";
I am trying the above code, but it's not working
If you look at the syntax highlighting, + count is being treated as a string.
You put it outside of the single quotes (kind of), but you still need to put it outside of the double quotes, which are the quotes you're really using to delimit the string:
document.getElementById("divProduct").style.backgroundImage="url('images/product" + count + "')";
You are also missing the image extension. It should be .png or .jpg or something else depending on whatever type your image is.
Also I'd like to know if you are getting any errors on the console.
Edit : For those looking for answers to this question, please also check blender's reply below mine.
Your code should be like Blender said and still if it doesn't work add in you div
sample:
<div id="divProduct"> </div>
I think it must be like;
var count;
document.getElementById("divProduct").style.backgroundImage="url('images/product" + count + ".jpg')";

Error in javascript function

I am using the following code
function xhi(aax)
{
var aby=document.getElementById(aax);
aby.style.bottom=(parseInt(aby.style.bottom)+(screen.height-42)/10)+'px';
if(parseInt(aby.style.bottom)<(screen.height-42))setTimeout('xhi("'+aax+'")',25);
}
When i run this code the function calls itself only two times . second time aby.style.bottom becomes Null.Why?
Check the bottom value. It might be crazy, but if the value is something like 008, 010, etc.
parseInt treats the number as octal. In order to avoid this, use :
parseInt(aby.style.bottom,10)
Why are you using bottom? I believe the best approach is the top attribute.
If everything else fails, jQuery has some nice functions to animate and to grab those style attributes.
I am not sure but try after removing from your code +'px' .

Categories