Programatically click a button on webpage? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Here is the button code:
<button class="ncss-btn-black fs16-sm ncss-brand pb3-sm pl5-sm pr5-sm pt3-sm mr3-sm u-uppercase css-1n4ymyz" data-test="qa-cart-checkout">Checkout</button>
I have tried the following;
document.querySelector("button[data-test='qa-cart-checkout']”).click();
But I get the following error;
Uncaught SyntaxError: Invalid or unexpected token
What am I doing wrong? Please help, thanks.
I need the answer in modern dom or jquery.

It works fine as long as you use "normal" single and double quotes, not backticks or "smart quotes":
document.querySelector("button[data-test='qa-cart-checkout']").click();
<button class="ncss-btn-black fs16-sm ncss-brand pb3-sm pl5-sm pr5-sm pt3-sm mr3-sm u-uppercase css-1n4ymyz" data-test="qa-cart-checkout" onclick="alert('it works')">Checkout</button>

In Javascript a Backtick means template literals.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
use regular ' or "

Related

QuerySelectorAll will not work as expected on an img alt [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Here is the line of code I am trying to target: <img class="pi" src="/graphics/ni/ni90.jpg" alt="Awaiting image">
I'm trying to select all of the images that have the attribute alt="Awaiting image"
If I try document.querySelectorAll("img.pi");
I will get all of the images that have the class pi
If I try document.querySelectorAll("img.pi[alt="Awaiting image"]");
I will get the error:Uncaught SyntaxError: missing ) after argument list
What is it that I am missing here?
The " used to delimit the value in the attribute selector are clashing with the " used to delimit the JS string literal.
document.querySelectorAll("img.pi[alt="Awaiting image"]");
^ ^^
| |Error
| End of string
Start of string
Use a different kind of quote.
document.querySelectorAll('img.pi[alt="Awaiting image"]');

JavaScript giving nonsense errors [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So, me and a few friends are making a text adventure in JavaScript. in it, there is a possibility of the player using a heal spell, which runs the following code:
function heal() {
alert('You chant some ominous-sounding words, and your wounds heal');
alert('Hit points restored!');
hitPoints += 60;
blobsOfDoom -= 30;
burn();
MonsAtt();
Choose Spell();
}
Error Messages:
Firefox:
/*
Exception: SyntaxError: missing ; before statement
#Scratchpad/1:2703
*/
Chrome:
SyntaxError: Unexpected identifier.
Why is this error showing up? How do I fix it?
You cant have spaces in functions
Choose Spell();
Check your function declaration for ChooseSpell and any other occurances, and change them to a valid function name, like chooseSpell()

Uncaught SyntaxError: missing ) after argument list in attribute selector [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
$(document).ready(function(){
var email_default = "Enter your email address...";
$("input[type="email"]").attr("value", email_default);
});
Javascript console comes up with this:
Uncaught SyntaxError: missing ) after argument list
HTML looks like this
<p>Name: <br><input type="text"/></p>
<p>Email: <br><input type="email" size="30" /></p>
As you can see from the syntax highlighting, you need to either escape the quotes in the attribute selector or alternate them. Try this:
$('input[type="email"]').attr("value", email_default);

Why does JS throw unexpected error while re-writing a variable? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Error is:
function (a){var b,c,d,e=this[0];{if(arguments.length)return d=m.isFunction(a),this.each(function(c){var e;1===this.nodeType&&(e=d?a.call(this,c,m(this).val()):a,null==e?e="":"number"==typeof e?e+="":m.isArray(e)&&(e=m.map(e,function(a){return null==a?"":a+""})),b=m.valHooks[this.type]||m.valHooks[this.nodeName.toLowerCase()],b&&"set"in b&&void 0!==b.set(this,e,"value")||(this.value=e))});if(e)return b=m.valHooks[e.type]||m.valHooks[e.nodeName.toLowerCase()],b&&"get"in b&&void 0!==(c=b.get(e,"value"))?c:(c=e.value,"string"==typeof c?c.replace(lc,""):null==c?"":c)}}
What I am trying to do is to catch a variable on Focus Out:
$('.field').focusout(function() {
var date = $('.field').val;
console.log(date);
//Do something else
});
The HTML for field is:
<input type="text" class="field" />
Just a Typo...
val is a function. Use val()
Yes I know this question is not worth an answer, but otherwise someone will just transplant the comment as their own answer :)

Why am I unable to convert this json into a string? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have json data I'm having difficulty turning into a string. How can I format it so that it can become strings?
jsfiddle: http://jsfiddle.net/s97QX/2/
code:
Jsonvar = {"shows":[{"show_id":6387, "shownum":6387,"title":"The Protestant's Dilemma","guest":"Devin Rose","category":"Non-Catholic","url":"http://www.catholic.com/radio/shows/the-protestants-dilemma-11565","audiourl":"http://www.catholic.com/sites /default/files/audio/radioshows/ca140331b.mp3","datetime":"1396317600","description":"
Devin Rose <\/p>","thumbnailsmall":"http://www.catholic.com/sites/default/files/imagecache/profile_square_small/images/profilepics/a109aad8daa70ad8976ffc.L._V387899120_SX200_.jpg","thumbnaillarge":"http://www.catholic.com/sites/default/files/imagecache/profile_square_large/images/profilepics /a109aad8daa70ad8976ffc.L._V387899120_SX200_.jpg"}]};
var jsonstr=JSON.stringify(Jsonvar);
alert(jsonstr);
First You validate your json using jsonlint.
JSONLINT
then try.
This error in the console explains it:
Uncaught SyntaxError: Unexpected token ILLEGAL
You have a line break in your object:
Jsonvar = {"shows":[{"show_id":6387, "shownum":6387,"title":"The Protestant's Dilemma","guest":"Devin Rose","category":"Non-
Catholic","url":"http://www.catholic.com/radio/shows/the-protestants-dilemma-
11565","audiourl":"http://www.catholic.com/sites/default/files/audio/radioshows/ca140331b.mp3"
,"datetime":"1396317600","description":" <<<- Here
Devin Rose
Remove that and that property will become a valid string, therefore you'll be able to stringify it

Categories