Write correctly class name inside onclick of HTML element - javascript

I have a class named validate['required'] and I need to put it inside
onclick="something();document.getElementById('something').setAttribute('class',here);"
the problem is that I get a syntax error : missing ) after argument list.

As per your comments I guess you are doing:
onclick="something();document.getElementById('something').setAttribute('class','vaildate['required']');"
The string between onclick="..." is parsed by the JavaScript interpreter of the browser.
Of the JavaScript code, this fails:
'vaildate['required']'
because the JavaScript interpreter will think everything between ' and ' will be your class name, so in your case:
vaildate[
Then the interpreter also reads required']' which it does not know how to handle, thus producing an error.
To literally use the ' character without the browser using it as the end, you can escape it by prepending \:
'vaildate[\'required\']'

Do you mean something like this?
onclick="something();document.getElementById('something').setAttribute('class','validate[\'required\']');"

Related

Adding JavaScript Function with arguments to an element from code behind

Hi Guys I've been dealing with an estrange thing while trying to pass string parameters to a JavaScript function from code behind, this is what I have actually in code behind which is wrong:
thumbnail = "<a href = 'javascript:RemovePirctureDetail(" + field1 + ",'" + tempname + "');' class='label label-default' rel='tooltip'>Remove</a>";
So this is the bad result I'm getting in the browser:
Remove
Meas that for some reason when I try to pass the string parameter, the html comes out bad formatted. The result should looks like this:
Remove
I tried already send the quotation marks like this /' from code behind, it did not work neither. How can I achieve this?
Thanks.
string thumbnail = "Remove";`
You need to use \ to escape the quotes inside, not /..
With javascript attribute I wouldn't use single quote, because it could be messy
Try to change in this way:
thumbnail = "Remove";
PS: Actually, I would NEVER use single quotes with attributes, it will cause an HTML validation error, HTML is not well formed with single quotes for attributes (although with inspection tools you see double quotes.. because inspection tools have the need to work with a well formed HTML, so if you want to see the real HTML of your page, view the source code (generally the option is on right-click on the page)

jQuery string escape onclick='deleteRow(item"+count+")' how to quote around item and count

I don't even know how to ask this question correctly, I tried to put quotes around a part of my string however it always break.
I am creating html dynamically and I am encountering error when I try to do this:
onclick='deleteRow("item"+count+"")'
I am trying to pass item1 as a string to a deleteRow function however best I could do is pass it like this deleteRow(item1) with no quotes. I am not sure how to escape them so that they would show.
This line of code is generated inside my JavaScript file.
I would recommend to use jQuery event handlers instead of inline one..
But in this case the below should do it
"onclick='deleteRow(\"item"+count + "\")'"

SyntaxError: identifier starts immediately after numeric literal in Firebug

I'm getting that error when I call this javascript function:
function kickUser(id_userChat){
$.post("chatFuncs.php", { action: "kick", id_user: id_userChat });
}
this "kickUser" function is generated for every user connected to my chat box, like this
$listUsers .= '<img src="imgUsers/'.$DBClass->nomImg($rowUsers['id_user'],$posImg).'" height="'.$heightImg.'" width="'.$widhImg.'"/>
<span class="styleMsg">'.$rowUser['nameUser'].'</span>
Kick</br>';
and the action "kick" is just an update to my database where I remove the user from my chatUsers table
If I change $rowUsers['id_user'] for $rowUsers['userName'] the error changes to:
ReferenceError: 'userName' is not defined (i changed the real name of the user for 'userName' just for this example).
Identifiers in JavaScript can't begin with a number; they must begin with a letter, $ or _.
I'm guessing it's coming from this:
onclick="kick_user('.$rowUsers['id_user'].')">Kick</a>
If you mean to pass a string, then you need to quote the value being passed.
onclick="kick_user(\"'.$rowUsers['id_user'].'\")">Kick</a>
I don't know PHP, so maybe you need different escaping, but this should give you the idea.
The resulting JavaScript code will be
kickUser(userName)
…and obviously there is no js variable userName. You want to pass a string instead:
kickUser('userName');
So add the quotes/apostrophes to the output, and don't forget to escape the $rowUsers['userName'] properly. It's quite the same for $rowUsers['id_user'], which seems to have output even an invalid identifier.

run jQuery function by inline onClick and use function variable/reference

I am very new to this, but I wrote this and thought it would work first time, but I get an 'ReferenceError: Can't find variable: street' console error.
I get this error if I click on the 'Street' button.
It's quite basic but this is the first time I've made a function to use a var/ref from the onClick attribute.
Please see onClick markup...
Supersport
Street
Cruiser
Scooter
Motocross
Enduro
Kids
then please see my function, which gets the ref error above...
Also please note I am trying to use the onClick var/ref within my function so I can target specific elements relative to the button being clicked.
bikeFilter = function (y) {
$('.bike').fadeOut();
scrollTo(186);
$('.bike[data-group=' + y + ']').fadeIn();
bikeSliderNav();
bikeSlider();
return false;
}
Any expert advice would be really helpful.
Thanks in advance.
You'd probably wanna pass a String as input to your function and not the name of an undeclared and uninstantiated variable.
Try to use the single quotes to refer it as a String constant (you need single quotes since you are already using double quotes to tell your html tag the attribute value):
onclick="bikeFilter('scooter')"
Take a look here to see the difference in data typing in js, and here for a quick start about functions.
You should use them like :
onclick="bikeFilter('motocross')"
Don't forget to put ' around your parameters

Pass variable to a javascript function in a c# code

I need to pass a variable to a javascript function,but I got a little trouble.
In the .cs file,I write like this:
string id = "someid";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction("+id+")\">"));
I need to use the value of this id,but in the console.log(id),it just shows "object",not the "someid",what's the problem?
Look at the generated HTML:
<input type="button" onClick="myFunction(someid)">
You are generating a variable name when you want a string literal.
Add some quote marks.
Whenever you have a problem that manifests in the browser: Look at the code the browser is dealing with first. You should always start by determining if the JS you want is not working or if the server side code is not generating the JS you want.
just add '' around id as i did below will resolve your issue
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
Use the single quotes around id:
'"+id+"' (notice the single quotes then double quotes)
The problem is that it is looking for a saved variable on the page named someid, which is why you are getting the object in the console. Pranay Rana has a good solution which is to make sure the string someid is surrounded by single quotes.

Categories

Resources