I am getting syntax error
Uncaught SyntaxError: missing ) after argument list
My code
<button onclick="alert('document.getElementById('todo.age').value');">onclick</button>
in controller add
$scope.alert = function(age) {
alert(age);
}
in html change like this:
<button ng-click="alert(todo.age);">onclick</button>
Try this:
<button onclick="alert(document.getElementById('todo.age').value);">onclick</button>
You shouldn't quote the argument to alert if you want to call the function.
It will start working better if you separate your HTML code and JavaScript.
Kind of this.
HTML:
<button id="button">onclick</button>
JavaScript:
window.onload = function()
{
var button = document.getElementById('button');
button.onclick = function()
{
//do some action here
}
}
This will help you to avoid simple syntax errors.
if u need value of the todo.age id values use the following code. <button onclick="alert(document.getElementById('todo.age').value);">onclick</button>
or else of u want to print the whole text document.getElementById('todo.age').value like this
use
<button onclick="alert('document.getElementById('todo.age').value');">onclick</button> code/
Related
I want to pass a field of a domain object to a javascript function in my view.gsp (grails) , but I am getting a syntax error.
Here is my gsp and javascript - please let me know if you see the syntax error. Thanks!
/*HTML*/
<td>${fieldValue(bean: studentInstance, field: "active")}</td>
/*JS*/
<script type="text/javascript">
var id = 0;
function setID(userId){
console.log("userId: " + userId);
id = userId;
}
</script>
The issue is you have function in your onclick. You don't need it there. Remove it so your onclick looks like this:
onclick="setID( ${studentInstance.id})"
I've got a feeling this is an incredibly simple operation. I could just use some steer on what exactly I am doing wrong here.
I just need a simple program to take the content from <span id "text"> and copy this into <div id="text2">.
function getValue() {
var text = document.getElementById("text");
document.getElementById("text2").innerHTML = text;
};
getValue;
<span id="text"> Hi there </span>
<div id="text2"></div>
It seems that you are not calling function properly.
getValue();
instead of
getValue;
Use this code:
function getValue(){
document.getElementById("text2").innerHTML = document.getElementById("text").innerHTML;
}
getValue();
This code should be in a onload function or in the body tag.
try following:
function getValue()
{
var text=document.getElementById("text").innerHTML;
document.getElementById("text2").innerHTML = text;
};
try this jQuery code...
function getValue(){
$("#text2").text($("#text").text());
}
this works for me...
I know this is a silly question but i am not able to get the required result.I want to assign a javascript variable bck in document.getElementById(bck) .Everything is working fine i.e. alert displaying the correct value of variable bck but when i am using it inside the document.getElementbyID i am getting the following error:
document.getElementById(bck) is null
I googled it and looked in SO relevant topics also but got nothing helpful.
the value of backdropcontent[m][1] is Reden,also the value of selectedbg is Reden.
<script>
for ( var m=0;m<backdropcontent.length;m++) {
if(selectedbg==backdropcontent[m][1]){
var bck=backdropcontent[m][1]+'div1';
alert(bck);
document.getElementById(bck).style.display = "block";
document.getElementById(bck).style.top = "0px";
}
}
</script>
html part:
<div class="mcdropdown" id="Redendiv1" style="display:none;position:relative">
<a style="cursor: default !important">
<input type="text" name="reden1" id="reden1" style="background-image: url('<?php echo $mosConfig_live_site; ?>/templates/performitor/images/123.png');background-repeat: no-repeat;height:14px;width:130px !important;color:#BDBDBD;border: 1px solid #8e9daa;" disabled="disabled" value="Totaal" autocomplete="off"/>
</a>
</div>
please note that i dont want to alter the structure of my code so please dont suggest any major change.
Any help will be appreciated.Thanks.
The element with id backdropcontent[m][1]+'div1', does not exist
It's throwing error mostly because your element doesn't exist on the page yet. Move your <script> block below your code or use window.onload event.
window.onload = function(){
//your code
}
Or using jquery:
$(document).ready(function() {
// your code
});
you use the code in the following pattern
<script>
for ( var m=0;m<backdropcontent.length;m++) {
if(selectedbg==backdropcontent[m][1]){
var bck=backdropcontent[m][1]+'div1';
alert(bck);
document.getElementById("bck");.style.display = "block";
document.getElementById(bck).style.top = "0px";
}
}
</script>
May be this code helps you.
Before I start, no I have no issues that I can find with semicolons, and I'm passing NO values to the function.
When I try to do function "login()" from the console, it works just fine, but when I click an HTML input button to call it, I get "Uncaught TypeError: object is not a function". This is in Chrome by the way.
var aj=new XMLHttpRequest();
var loginf=document.forms["login"];
var regf=document.forms["reg"];
function login(){
var errors=document.getElementById("login_err");
var errs=[];
var uname=loginf['uname'];
var pass=loginf['pass'];
var unameVal=uname.value;
var passVal=pass.value;
if(unameVal.length<4 && unameVal.length>0){
errs.push("Username too short. Try again please.");
}
else if(unameVal.length>18){
errs.push(innerHTML="Username is too long. Try again please.");
}
else if(unameVal.length==0){
errs.push("Please enter a username.");
}
if(passVal.length<8){
errs.push("Password too short. Try again, please.");
}
else if(passVal.length>30){
errs.push("Password is too long. Try again please.");
}
if(errs.length==0){
aj.onreadystatechange=function(){
if(aj.readyState>=4 && aj.status==200){
if(aj.responseText=="suc"){
window.location="/new.php";
}
}
}
aj.open("POST","/inc/php/core.php",false);
aj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
aj.send("login=true&data="+data);
}
else{
errors.innerHTML="<ul>";
for(var x=0;x<errs.length;x++){
errors.innerHTML+="<li>"+errs[x]+"</li>";
}
errors.innerHTML+="</ul>";
}
}
function reg(){
}
And then the input button..
<input type="button" class="submit" value="Log in" style="width:25%" onclick="login();"/>
I see nothing wrong with the code.
Thank you in advance if you can find Waldo in this code.
jsFiddle HERE: http://jsfiddle.net/6sa7M/2
It turns out that the function name "login" was both a reference to the form and function.
I changed "login()" to "loginUser()", and typed "login" into the console, and the form was returned, so they were indeed conflicting with each other.
Thank you again for all the help and special thanks to Marc for the true answer.
I need to do some ugly stuff. But I cannot do it another way. what I need is
<a href='some/url/?with¶ms' onclick="(if confirm('Do you wanna submit?')
{some code to submit form in href property})">
but I don't know how to make inline script work... and I hesitate about the way of submitting it with window.location or document.location. Any ideas?
Try this:
on html:
Submit
<script type="text/javascript" src="myValidation.js" />
myValidation.js
I used json and javascript closures on this answer.
; //starts with this to close other js not yet closed
var MyValidation = {
actionSubmit : function() {
//your code go here
//example of calling another function
MyValidation.anotherFunction();
//example of accessing a global variable (for your validation)
MyValidation.variable;
//example of declaring local variable
var word = "hello";
//example of calling another function with parameters
MyValidation.anotherFunctionWithParameters(word, MyValidation.variable);
//the returning
return MyValidation.someValidation(word);
}
//Yes, this is a comma.
//I'm putting in the beginning so we see this and didn't forget :)
,anotherFunction : function() {
//some code
//maybe a return
}
,variable : {}
,anotherFunctionWithParameters : function(param1, param2) {
//more code, maybe a return
}
,someValidation : function(parameter) {
//some validation
return true|false;
}
};
Take a look on this:
How do JavaScript closures work?
json
Have you heard about AJAX? Google it if you don't.
The onClick parameter accepts a function name with or without parameters, not "inline scripts".
<script type=text/javascript>
function doStuff(){
if(confirm("Submit?"){
//do your stuff with AJAX to some/url/?with¶ms
}
}
</script>
<a href='javascript:doStuff'>link</a>