How to add a variable into a jQuery addClass function [duplicate] - javascript

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I am very new to jQuery but learning. I am trying to do something simple (I think) but struggling. I am trying to add a var into Jquery to customise an specific element.
I need to add 'styler' class to my div which has the id="spinner7" like -
$('#spinner7').addClass('styler');
However the number can vary so I need to add it to the end of #spinner. I have tried things like below but no joy. Can anyone help please.
var ctrl = '7';
$('#spinner +ctrl').addClass('styler');

Template literals
var ctrl = '7';
$(`#spinner${ctrl}`).addClass('styler');
Or
var ctrl = '7';
$('#spinner' + ctrl).addClass('styler');

Related

How to add “ ?&max-results= ” after the /search/label/LABEL_NAME in blogger use javascript [duplicate]

This question already has answers here:
How to update (append to) an href in jquery?
(4 answers)
Append URL parameter to href with JavaScript
(2 answers)
How to append to an anchor's href attribute with JavaScript but not affect query string?
(3 answers)
Closed 2 years ago.
I want to change all the labels links and have a specific number to access it in blogger using JavaScript automatically
An illustrative example
../search/label/Label_name and add max-results=7 after "label name"
how i can do it .. i want help and thank you.
Something like this should do the trick. If you have further questions feel free to ask them :)
var x = document.querySelectorAll("a");
x.forEach(function(element){
var link = element.href;
console.log(link)
element.href = link + "?max-results=7";
console.log(element.href);
});
Example link

jquery - how to add an variable as an id in jquery command.? [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
Its a simple question, could not find an answer from google.
Code
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID input).attr("checked",true);
});
Onclick function is giving me the parent id, Now, using the parent id i want to find the input element and add checked attribute.
I am not sure of the syntax of querying by dynamic ID.
I want to know how can i query by dynamic variable.
Thanks in advance.
$("#"+curID).find('input').attr("checked", true);
Or
$(this).parent().find('input').attr("checked", true);
Or
$('input', $(this).parent()).find('input').attr("checked", true); // using the scope argument
The selectors are strings... So should be handled as strings by concatenating the variables: and texts
$.click(function(){
var curID = $(this).parent()[0].id;
$("#"+curID+" input").attr("checked",true);
});
Your search is probably too specific. Break tasks down into their components instead of looking for a complete solution to a very specific problem. You are just dealing with basic string concatenation here.
You want:
var selector = "#foo input";
You have foo in a variable.
var selector = "#" + foo_variable + " input";

empty jquery element [duplicate]

This question already has answers here:
Getting an empty JQuery object
(4 answers)
Closed 8 years ago.
Is there an elegant way to create an empty jquery element (versus null) ?
$myEmptyElement = $("#ThisIdDoesNotExist234343");
The rational is not checking later on for null.
Later we do :
$myEmptyElement.destroy();
Sure:
var $emptyElement = $();
Why would you want one, though?
A simple example would be:
var $emptyElement = $();
One way:
var $emptyElement = new $;

How to pass global JS var within regexp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you pass a variable to a Regular Expression JavaScript?
This is my regexp
regexp:{
spamcheck : /^[15]+$/
}
I need to do this
var spamcheck_sum = 15;
regexp:{
spamcheck : /^[spamcheck_sum]+$/
}
and
Side note: I cant use my spamcheck_sum var outside the js reason is my js file loads first and my var for sum loads after , any way to globalize that var to be acceptable no matter if js file loads first?
working with cms here and js file placement is automatic if I use the cms js placements calls . Joomla
var spamcheck_sum = 15;
regexp:{
spamcheck : new RegExp("^["+spamcheck_sum+"]+$")
}

Is there a += available for attr() and val()? [duplicate]

This question already has answers here:
Is it possible to do ".value +=" in JQuery?
(5 answers)
Closed 8 years ago.
Since I've moved to jQuery development has sped up for a great deal, but there is one thing which has become a little more time-consuming: appending some string to an attribute/value.
For example, if you wanted to append a letter to the value of a textbox, you would normally do:
input.value += 'a';
but now using jQuery, it has become:
input.val(input.val() + 'a');
I know it wouldn't be too hard to create a function which can do this and then append that function to jQuery.fn, but I was rather wondering whether there is such a function perhaps already available. I haven't found any so far. Or is there perhaps a better technique of appending a string to a value/attribute than what I'm doing at the moment?
If you know that your element has a value attribute, there is no reason not to use
input.value += 'a';
There is nothing to stop you using plain JavaScript in your jQuery scripts.
In fact,
input.val(input.val() + 'a');
has no benefits over using plain 'js', except if your input is a jQuery object and you want to chain methods.
I'd suggest overall
var $input = $('#someInput');
$input[0].value += 'a'; //get the DOM object from your jQuery object

Categories

Resources