Show more than 10,000 characters in a JavaScript Alert [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript object max size limit at 10000 chars
I want to check whether html source contains a specified string, but I'm only getting 10,000 characters (in alert box)
var str=document.documentElement.innerHTML;
if(str.indexOf("abcxyz") !== -1)
{
alert(str);
}
How can I fix that?

If you need to display a huge string in alert boxes, I guess you could split it into 10,000-character chunks and display them in order. Alternatively, just use console.log to print it out.
And is there a better way to detect a "onClick" event?
Better than what? JavaScript events will bubble up to the top containing element (unless some element in the chain calls stopPropagation()), so an easy way to detect click events would be to attach a click handler to document.body:
document.body.onclick = function() { alert('Click!'); }

Related

JavaScript Puppeteer: Check if element is present on webpage [duplicate]

This question already has answers here:
How can I check if selector exists in Puppeteer?
(8 answers)
Closed 7 months ago.
I am using the JavaScript Puppeteer API. I need to check if an element(a button) is available on a webpage so I am able to click it without the worry of it giving me an error. Sometimes the element won't appear, so knowing it's there will help me to know when to click it.
Thanks for any help!
The Page.$ function will allow you query the page for an element by its selector. If it is there it will return an "element handler" if it is not present on the page it will return null. Remember to await it since this like most puppeteer functions is async.
https://pptr.dev/api/puppeteer.page._
Example:
const element = await page.$('.my-selector');
if(element !== null){
//click on it
}
`

Simplest way to escape all special chars of user input text [duplicate]

This question already has answers here:
Sanitizing user input before adding it to the DOM in Javascript
(7 answers)
Closed 5 years ago.
I am adding a option to a select that is input by a user:
function prependToSelect(userInput){
$('#x').prepend('<option>' + userInput + '</option>');
}
what is the safest and most simple way to make sure there is nothing dangerous in userInput that can break the javascript or html? It would also be good if the solution/function would be reusable in different scenarios.
My inital answer would be to create a options element and set the innerText instead of innerHTML
but since u use jQuery...
$('#x').prepend($('<option>', {text: userInput}))
Don't add it as HTML.
document.querySelector("#x")
.appendChild(document.createElement("option"))
.textContent = userInput;
This adds it as .textContent so no HTML will be parsed, so no danger.

single addEventListener with Multiple selects [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 5 years ago.
I have looked around and I haven't really been able to find anything that solves my problem. There is a post using buttons but I can't seem to modify it for my needs.
I am trying to use only one event listener for multiple selects. I'd like to return the id of the select, and the value selected but as far as I can figure I either need to use document.getElementById("ID").addEventListener('change',func(),true) for each id or document.getElementsByTagName('select').addEventListener('change', func(), true) and I get an error that says:
selection.addEventListener is not a function. (In 'selection.addEventListener('change', func(), true)', 'selection.addEventListener' is undefined)
I was hoping someone could take a moment and show me where I am going wrong or if I need to use a different method to accomplish my task.
thanks for the help
The method getElementsByTagName returns HTMLCollection of elements, and not a DOM Element, so you can't use addEventListener on that.
What you can do is go over all the elements in the HTMLCollection and add the event you want to them:
let selectElements = document.getElementsByTagName('select');
Array.prototype.forEach.call(selectElements, function(el) {
eladdEventListener('change', func(), true)
})

Clicking on an element with a variable attribute value in a certain format with CasperJS [duplicate]

This question already has an answer here:
Is it possible to use a CSS wildcard in the middle of an attribute selector?
(1 answer)
Closed 6 years ago.
I have that code snippet to click a button within CasperJS.
this.click('input[name="suggestion-to-repeat:5:suggestion:subForm:select-email"]');
The Problem is, there is an variable Number in the Name. In this example the "5". The Number can be between 1-10. Is there any chance to work with wildcards or any other possibilities to select the button?
Have a look to https://www.w3.org/TR/selectors/#selectors The following may help:
E[foo^="bar"]an E element whose "foo" attribute value begins exactly with the string "bar"
E[foo$="bar"]an E element whose "foo" attribute value ends exactly with the string "bar"
Try:
'input[Name^="suggestion-to-repeat:"][Name$=":suggestion:subForm:select-email"]'

Do something when clicking on class [duplicate]

This question already has an answer here:
How do you assign an event handler to multiple elements in JavaScript?
(1 answer)
Closed 7 years ago.
I am trying to run a function when user clicks on a class.
Here is a fiddle with a similar setup. Except that I am not using buttons in the my code.
FIDDLE
document.querySelectorAll('.menu').onclick = function () { alert("test"); };
I've also tried using the getElementsByClassName, with the same results. Is there something I am missing here?
*Note: I need to accomplish this without jQuery
querySelectorAll returns a list of elements, so you need to specify the one you want, in your case [0]:
document.querySelectorAll('.menu')[0].onclick = function () {
alert("test");
};
jsFiddle example

Categories

Resources