adding second to my JavaScript [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
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).

Related

there's an error to createElement and its attribute [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 3 years ago.
Improve this question
I try to createElement as h1 and its attribute class, but i can not get it working.
here goes my post:
function krea_ta(){
var tagy = document.cteateElement("h1");
var clasy = createAttribute("class");
clasy.value = "myclass";
tagy.setAttributeNode(clasy);
tagy.innerText = "business";
}
Where is the problem?
First things first, you've spelled "create" wrong on "createElement".
Secondly, you can either set the class as an attribute using tagy.setAttribute("class", "myclass")
or via the classList property which expects an array of strings..
tagy.classList = ["myclass"];
Edit:
The above assign of the array might not be working on all browsers (works on Chrome), as MDN states that classList is a read-only property.
tagy.classList.add(["myclass"]);
The .add method accepts parameterised values, array of values and a single value.
Why not simply do it like this:
function krea_ta(){
const tagy = document.createElement("h1");
const tagyText = document.createTextNode("business");
tagyText.appendChild(tagyText);
tagy.classList.add("myClass");
document.body.appendChild(tagy);
}

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.

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';

how to insert variable into jquery selector [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
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.

How to get the final result of 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 9 years ago.
Improve this question
I have an html page with a lot of javascript code, for example the content of a div depends on length of an array :
for (var i = 0; i < movieList.length; i++) {
document.body.appendChild(document.createElement('h2')).appendChild(document.createTextNode('title: ' + movieList[i].title));
var cUL = document.body.appendChild(document.createElement('ul'));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].rating));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].year));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].length));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode(movieList[i].isComedy));
cUL.appendChild(document.createElement('li')).appendChild(document.createTextNode('main characters: ' + movieList[i].mainCharacters.join(", ")));
}
I am using these perl LWPx::ParanoidAgent and HTML::TokeParser modules to handle the HTML code but the i want the result of the javascript script
You either need to:
Reverse engineer the JS and apply the changes it would make manually or
Run the HTML and JS through a browser or browser-like tool and read the data from its DOM
There are a number of options for the latter, including WWW::Mechanize::Firefox, WWW::Selenium and Wight.
perhaps https://getfirebug.com/ is what you're looking for. Amongst loads of other things you can view your HTML after JavaScript has altered it.

Categories

Resources