how to insert variable into jquery selector [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to insert a variable inside a jQuery selector like this example, how can I fix my problem?
var index = 5;
$(".tables li:nth-child('+index+')").addClass('vr_active');

The issue is in the quotes ...
var index = 5;
$(".tables li:nth-child("+index+")").addClass('vr_active');
This way, the index variable is part of a concatenation of:
".tables li:nth-child(" ... a string
index ... a variable
")" ... a string.
Functioning jsFiddle

try to use .eq()
var index = 5;
$(".tables > li").eq(index - 1).addClass('vr_active');

A jQuery selector is just a string.
So your problem is actually "How to insert a variable into a string" and this can be done in a myriad of ways, for example:
".tables li:nth-child(" + index + ")"
var selector = ".tables li:nth-child(${index})";
selector = selector.replace("${index}", index);
etc.

Related

how to pass a variable to queryselectorAll? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hi I want to pass a variable to queryselectorAll. I tried a couple of things but non have worked for me. I'm doing that because in my code I change the variable to a new one every time a click a button.
var container = document.getElementById(containerBox.id)
// I want containerBox.id to be called in querySelectorAll
templateDiv = document.querySelectorAll('#' + 'containerBox.id' + 'template')[0].content.firstElementChild
Thanks in advance
This question can be simplified to: How do I construct a string from fixed parts and a variable part?
The answer to that is:
'#' + containerBox.id + 'template'
(i.e. just don't put quotes around your variable name).
But why bother using .querySelectorAll() just to grab the first index? You could simply call .querySelector() instead.
And if all you need to get is an element with a specified id, you can just do this:
document.getElementById(containerBox.id + 'template')
... which is the method you're already using in your first line.

Need to replace numbers between [...] with Javascript or jQuery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Inside a string: I need to replace the number between [ ] by another number with Javascript or jQuery.
What is the best way to proceed ?
Eamples
"[12345].Checked" > "[1].Checked"
"[123].RequestID" > "[21].RequestID"
Thanks for your help.
function fixNum(str, newnum) {
return str.replace(/\[\d+\]/,'[' + newnum + ']')
}
Use .replace:
"[12345].Checked".replace(/\[(\d+)\]/, "[1]") // "[1].Checked"
With regular expressions:
/\[(\d+)\]/
It searches a number (no matter length) inside a [] set.
Combined with replace you can make it.
Test it:
https://regex101.com/r/zT5kD0/1
Use replace method to replace the part
function replaceStr(string,orgText,replaceText){
var res = string.replace(orgText, replaceText);
return res;
}
console.log(replaceStr("[12345].Checked",'12345','1'));
jsfiddle

Jquery not disabled the parent [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Input tag not disabled.This is my code.Please help me.
var input = $('input[value="' + $(this).val() + '"]');
input.prop('disabled', true);
input.parent('li').addClass('disabled');
You should use filter() to find the input with value instead of using Attribute value selector.
Reduce the set of matched elements to those that match the selector or pass the function's test.
Also you can use closest() to traverse up to desired parent.
Script
var value = $(this).val();
var input = $('input').filter(function(){
return value == $(this).val();
}).prop('disabled', true);
input.closest('li').addClass('disabled');

How to make the following JavaScript code work and result in a list of names and ages inside the element with ID "myDivId" : [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to make the following JavaScript code work and result in a list
of names and ages inside the element with ID "myDivId" :
<div id="myDivId">No results yet</div>
<script>
new PersonPrinter("myDivId",[{"name":"Joe","age":"23"}
{"name":"Sam","age":"31"}]).render();
</script>`
In the PersonPrinter render function you need to update the div
PersonPrinter.prototype.render = function(id,data) {
var elem = document.elementById(id);
for (var i =0;i <data.length;i++) {
var d = dcoument.createElement('div');
d.appendChild(document.createTextNode('Name'+data.name +' Age:'+data.age');
elem.appendChild(d);`enter code here`
}
}
You need to access div id using innerHTML
var a = Document.getElementById('myDinId');
And for changing value use simply = operator
a.innerHTML = 'Something';

adding second to my JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hello how can I add a second portion to my javascript. Here is the code:
var $pagerT = $('<div class="pager"></div>');
var $pagerB = $('<div class="pager"></div>');
for (var page = 0; page < numPages; page++) {
$('<span class="page-number"></span>').text(page + 1).bind('click', {
newPage: page
}, function(event) {
currentPage = event.data['newPage'];
$table.trigger('repaginate');
$(this).addClass('active').siblings().removeClass('active');
}).appendTo($pagerT).addClass('clickable');
}
Basically I want to add the same class that was added to $pagerT to $pagerB. Here is the code:
}).appendTo($pagerT, $pagerB).addClass('clickable');
Any subjections on how I can process it?
You can use .add
$pagerT.add($pagerB).addClass('clickable');
I'm going to say
var $pagers = $pagerT.add($pagerB);
...
}).appendTo($pagers);
$pagers.addClass('clickable');
The docs say that the argument to appendTo() can be an array of elements, and in that case, "cloned copies of the inserted element will be created for each target after the first".
(However I'm not sure if $pagerT.add($pagerB) creates an array (the doc says it creates a set); or more to the point, whether this value is acceptable as an argument to appendTo(). Testing this is left as an exercise to the reader.)
Or if you value brevity over maintainability,
...
}).appendTo($pagers.addClass('clickable'));
That's assuming that you want to add the <span> to both $pagerT and $pagerB (which you didn't say, but your second code example suggests) and add the 'clickable' class to both (which you said but which conflicts with your original code).

Categories

Resources