I want to create link that send to function:
tdLink2.innerText="Delete";
tdLink2.href="javascript:deleteDepartment(id)"
but the "id" parameter was not sent.
How can I insert the parameter?
ID is not parsed in your string
EITHER (Don't forget the extra quotes if ID is a string
tdLink2.href="javascript:deleteDepartment('"+id+"')"
alternative with template literals
tdLink2.href=`javascript:deleteDepartment('${id}')`;
I would personally keep DELETE far away from a href
This is better
tdLink2.href="#"
tdLink2.addEventListener("click",function(e){
e.preventDefault(); /stop the link
deleteDepartment(id); // id is some global variable
})
EVEN better is to do
tdLink2.href="#";
td.dataset.id = id; // assign to a data attribute
tdLink2.addEventListener("click",function(e){
e.preventDefault(); // stop the link
deleteDepartment(this.dataset.id); // pass the data attribute
})
If id is already a defined variable then you can do like this:
tdLink2.href=`javascript:deleteDepartment(${id})`
You can do this if it is of type string.
Otherwise you can go for this:
function f(){
deleteDepartment(id)
}
tdLink2.href='javascript:f()'
Just assign function result to property
tdLink2.href = deleteDepartment(id);
Related
I am trying to click on the first radio button and it will assign variables distSelected and weapon some values from the object engDistanceObject. I suspect my HTML might not be written correctly... specifically the input tags.
https://jsfiddle.net/Natronox/sbojaxm4/#&togetherjs=zb80KxkQzm
var engDistanceObject = {
short: ["100m-300m","416 Assault Rifle"],
long: ["300m-1000m","M110 DMR"]
};
var distSelected;
var weapon;
function distanceClick(item){
distSelected = engDistanceObject.item[0];
weapon = engDistanceObject.item[1];
console.log(distSelected,weapon);
};
Use your short and long properties instead of item. You don't have an item property name in your object.
.short[0]
.short[1]
To access dynamic properties, you can't use the . syntax. It ignores variables that might have the same name as the property you're trying to access, and instead will try to access the non-existent item property.
Instead, use the bracket syntax [], which allow a string to be used to access dynamic properties. This means that you need to pass a string as a parameter to your function.
Your HTML event handlers will need quotes around the parameters:
onclick="distanceClick('long')"
And then use the bracket syntax in your JS:
function distanceClick(item){
distSelected = engDistanceObject[item][0];
weapon = engDistanceObject[item][1];
console.log(distSelected, weapon);
}
I have input in grid, which each unique ID, output as following;
I want to know what is equivalent to $(this).attr("ID") in javaScript
function load_getUserListByGroupID()
{
var selectedGroupID = document.getElementById('input');
alert(selectedGroupID);
}
You can simply get it using .id attribute like this:
this.id;
Or you can also use .getAttribute() method:
this.getAttribute('id')
So your example should be, like this:
function load_getUserListByGroupID(element) {
alert(element.id);
}
aa
Pass this to onclick event handler. Then you can directly get its id.
Use
Script
function load_getUserListByGroupID(elem){
alert(elem.id);
}
Use a variable,
onClick="reply_click(this.id)"
function reply_click(clicked_id)
{
alert(clicked_id);
}
Add the OnClick function the element you'd like, this will also throw the ID value when processing to the function.
Hope this helps :)
I am making a div and in its onclick function I am calling function Remove() to which I am passing its id and name. But when I use name in my remove function, an undefined error is thrown. For example, I call Remove(1,xyz) and then in the Remove function I am unable to access xyz; it's showing me xyz is not defined.
Here is my jQuery code which calls the Remove function:
$("#h2-"+id).append("<div id = 'c2-"+id+"' onclick = 'Remove("+id+","+name+")' class='clear_btn1'> </div>");
and here is my Remove function:
function Remove(i, name){
alert("I am deleting " +name);
var sender = "<?php echo $user_check?>";
var receiver = name;
console.log("Name is " +sender);
console.log("receiver is " +receiver);
}
The value of i is coming perfectly fine but I cannot access name in my function.
Change your code as below
$("#h2-"+id).append("<div id = 'c2-"+id+"' onclick = \"Remove("+id+",'"+name+"')\" class='clear_btn1'>sdfsdf sdfsdf</div>");
What is saved in onclick is actually Remove(id,name). This looks okay at first look, but...
Say id=10 and name="Mark".
You would be calling Remove(10,Mark), which is not what you want. Mark would be treated as a variable. You therefore need to put additional quotes enclosing name to treat it as a string.
You should, of course, escape the additional quotes you would add.
You need to call Remove(10,"Mark"). Notice the quotes.
I am using an onsubmit variable to ensure that the user really means to delete something, however as soon as I put a value in the parenthesis inside the onsubmit it no longer calls the confirm box.
Code:
onClick="confirmSubmit(abc)"
Doesn't work but the following:
onClick="confirmSubmit()"
Does work
Function:
function confirmSubmit(category)
{
var category = category;
var agree=confirm("Are you sure you wish to DELETE" + category + " and all of its subcategories and photos?");
if (agree)
return true ;
else
return false ;
}
you need quotes around your abc:
onclick="confirmSubmit('abc')"
Without them you are trying to pass a variable, abc, which doesn't exist and triggers an error
onClick="confirmSubmit(abc)" is trying to pass the variable abc, if you intend to pass a string with the value "abc" then do this:
onClick="confirmSubmit('abc')"
function confirmSubmit(category)
{ var category = category;
And you've declared "category" twice! Once in the function header and then as a function variable in the next line! What for?
You're try to pass the variable abc (which does not exist) to the function.
Do:
onclick="return confirmSubmit('abc');"
I have a several input fields in a form each having a unique name. For example, to change the color I would do:
testForm.username.style.background = "yellow";
username being the name of the input and testform being the form name
I want to do this: replace username with a variable elem so that when I call the function to change the background color I don't need to have a separate function for every unique field. I would just send the elem name and that function would work for every field.
testForm.elem.style.background = "yellow";
My problem is it doesn't work. For example it passed the elem into the function fine, but it says that testForm.elem.style is null. For some reason javascript doesn't like variables for element names I'm guessing?
var elem = 'username';
testForm[elem].style.background = 'yellow';
try
testForm [elem].style.background = "yellow";
or
var elem = testForm.username
elem.style.background = "yellow";
in the first case elem holds the username, and in the second case, elem points to the actual DOM element.
A property of a JavaScript object (in this case, the "username" property of the object "testform") can be accessed using either object.property or object["property"] syntax. As the second form takes a string (as shown by the double quotes), it follows that a variable containing a string can also be used with that syntax. Hence:
testform[elem].style.background = "yellow";
will do what you want.
It sounds like you're creating a function to do this anyway. In that case, why not just use the following function:
function changeBackgroundOfElementToYellow(element){
element.style.background = "yellow";
}
And call it with:
changeBackgroundofElementToYellow(testForm.username);
In general I find the RaYell/kangax method posted already to be better, but this is another option.
You'd have to do an eval to do something like that, eg eval("testform." + elem + ".style.background=yellow");