why onclick not working as expected? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am confused why onclick is not wokring
Code1:
onclick="return clicked('35','http://www.google.com');"
Code2:
onclick='return clicked('35','http://www.google.com');'
If i used code1 onclick works fine but not with code2
thnx

Because you're breaking out of the onclick too soon, since you use single quotes for multiple things. The first one doesn't break since you can have single quotes in double quotes. If you use the second approach, escape the single quotes in the function call.

It's because of ' and " characters. If you open a " and want to put another string inside you need to use ' or it will close the first one.
That's why the code1 works, you start the string with ", then you use ' to specify parameters so the string isn't closed.
In code2, you start the string with ' and then use ' again to specify parameters. So the string you started is closed in the middle of you onclick statement.
code2 would work with :
onclick='return clicked("35","http://www.google.com");'

If you use single quotes for HTML attributes, you need to use double quotes for the Javascript arguments:
onclick='return clicked("35","http://www.google.com");'
Otherwise the browser is confused and thinks the attribute value is only return clicked(

Related

Additional Quotation marks besides " and '? [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
Are there additional quotation marks in Php, and JavaScript besides "..." and '...' in the case I need to nest them?
(The Alt+Number would be useful)
For example a Php echo:
echo "
onClick='changeImage('example.jpg');'
";
I know I could escape the quotation marks , but I am wondering if there are another "level" of them that can be nested.
Javascript
You are in luck, in ES2015 specs, javascript now allows template literals using back ticks.
console.log(`hello "world!" I'm doing well :)`);
PHP
Use Nowdocs. Don't use Heredocs unless you want to evaluate php code inside the string.
Note: someone already answered this, but I'll just reiterate to contain a whole solution.
echo <<<'STR'
`hello "world!" I'm doing well :)`
STR;
Don't use Heredocs
echo <<<STR
`hello "world!" I'm doing well :)`
STR;
Or
echo <<<"STR"
`hello "world!" I'm doing well :)`
STR;
Difference is the single quoted name. This is a Heredoc. It will evaluate PHP code denoted by ${expression} in your string.
Don't use back ticks
In PHP backticks will be evaluated as a shell command and return the output of said shell command.

In JavaScript, if multiple spaces are added between two strings why they get converted to a single space during code execution? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
While learning JavaScript I come across one issue. When I add multiple spaces between two strings, all spaces get converted to a single space. I tried to add multiple spaces between two strings with the help of concatenation operator(+). Also, I tried by adding multiple white spaces after the first string and before the second string but every time I get the same result. Two strings are separated by only one space in between them.
I searched for the cause but everyone is telling how to overcome this problem with the help of some regular expression and string replace function in JavaScript. I want the root cause of this problem.
I also know PHP. In PHP, this thing doesn't happen, PHP strings behave as expected by the developer. Whatever number of spaces I add in between, before or after the string they get added then why not in JavaScript?
Someone please explain me the cause and difference in this case between PHP and JavaScript.
I tried following expressions :
"John" + " " + "Doe";
"John " + " " + " Doe";
"John" + " " + " Doe";
"John Doe";
The output I got every time is : John Doe
The reason is not JavaScript, but HTML. The standard says that any number of whitespaces (spaces, linebreaks, etc ...) get reduced to a single space.
If you want to prevent this behavior, use .

Replace all String with empty string starting with -- [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i have string which is
testVariable--423h33c7uhyga5tjk
now i want to replace the above string with
testVariable
by using javascript replace function.
Use String#split method.
console.log(
'testVariable--423h33c7uhyga5tjk'.replace('--')[0]
)
Or with String#replace method.
console.log(
'testVariable--423h33c7uhyga5tjk'.replace(/--.*/, '')
// or including multiline
// .replace(/--[\s\S]*/, '')
)
While Pranav's method work, if you really need/Want to use the replace function, you could use regex:
var variable = 'testVariable--423h33c7uhyga5tjk';
console.log(variable.replace(/--.+$/, ''));

Concatenate HTML [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
I'm building my output text looping a JSON object using JavaScript. Everything is working fine until I come to the part where I need to add an ID number to an image URL. My images are stored in a database and I am using an ASHX handler to load the image however what I end up with is not exactly what I need.
My code that I need to end up with is ~/ImageHandler.ashx?id=35
but what I get is ~/ImageHandler.ashx?id='35'. Single quotes around the ID.
I know it is the syntax of " and "".
What I have tried is
myOutput += "<img src ='~/ImageHandler.ashx?id='" + ID + class='person-image'></img>"
and every combination that does not work.
If I remember correctly I need some combination of triple single quote or double quotes something or some combination of the both. ID is an integer that is being read from a JSON object.
Thanks in advance
You can add double quotes using the escape character \
myOutput += "<img src=\"~/ImageHandler.ashx?id=" + ID + "\" class=\"person-image\"></img>"
This will append
<img src="~/ImageHandler.ashx?id=35" class="person-image"></img>

Replace in comma followed by double quotes in javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Can anyone tell me how to replace comma followed by double quotes(",) with double quotes(") in java script
Actually I am getting the string as ",4,34,26,23"
but I want to remove the first comma in the string
also the same when it occurs at the last(,") as below
"4,34,23,54,"
Thanks in Advance
Rakesh
You can use regular expressions like this
var data = ",4,34,26,23,";
data = data.replace(/^,|,$/g, "");
console.log(data);
Output
4,34,26,23
If the double quotes are also part of the original string,
var data = "\",4,34,26,23,\"";
data = data.replace(/^",|,"$/g, "");
If you want to strip only the , and retain ", you can just put the double quotes as the second parameter to the replace, as suggested by #nnnnnn, like this
data = data.replace(/^,|,$/g, "\"");
data = data.replace(/^",|,"$/g, "\"");
var a = ",4,34,26,23";
var replaced=a.replace(',','');
alert(replaced);
Try this
var x = ',4,34,26,23';
x.replace(/^,|,$/g,'');
This removes any starting or ending commas :
",4,34,26,23,".replace(/^,|,$/g,"") // "4,34,26,23"
try this
var str = '",4,34,26,23"';
str = str.replace('",','"');

Categories