Pass a string parameter in an onclick function
It looks like you're building DOM elements from strings. You just need to add some quotes around result.name:
'<input type="button" onClick="gotoNode(\'' + result.name + '\')" />'
You should really be doing this with proper DOM methods though.
var inputElement = document.createElement('input');
inputElement.type = "button"
inputElement.addEventListener('click', function(){
gotoNode(result.name);
});
document.body.appendChild(inputElement);
Just be aware that if this is a loop or something, result
will change before the event fires and you'd need to create an additional scope bubble to shadow the changing variable.
how to pass string in onclick function html
<a href="#" onclick="test('TEST123')">
just wite string parameter in your js function
Can I pass a string argument to an onclick javascript function?
You can do it in 2 ways. You can either call the function from the element itself like:
<a href="javascript:void;" onclick="myFunction ('string')" id="druck">
Or you can use a data-* attribute;
<a href="javascript:void;" data-string="my string" id="druck">
And inside js,
b.onclick = (event) => {
let myString = event.currentTarget.dataset.string;
//Code
}
(https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset)
OnClick: Passing String as Parameter
You have to add quotes to the string parameter:
<div id="menubutton1" class="menubutton lightbox" onclick="lightbox(1000, 410, 'hello')">
will work.
How to pass string parameter through onclick
- You have extra
"
hereid="sp_'.$app_id.'" "
. - Pass the second parameter as string so you miss double quotes.
- Internal double quotes should be escaped like this
"foo \" foo"
. - You can pass variable in strings with double quotes
"$variable"
.
so your code should be like this
$table.=" <td><input type='checkbox' value='' id='sp_$a' onclick='SpDetails($a,\"$b\");' /></td>";
echo $table;
how to pass string parameter in onclick function
Try this code snippet:
<%
String status = request.getParameter("status");
String id = request.getParameter("id");
String value = null;
String func = null;
if (status.equals("approve")) {
value = "update";
func = "updatestatus";
} else if (status.equals("reject")) {
value = "delete";
func = "deletecompany";
}
%>
<input type='button' value='<%=value%>' onclick='<%=func%>("<%=id%>")'/>
PS Use JSTL instead of jsp scriptlets.
How to pass a parameter to function which is in onclick and the onclick is in high ordered map. Does anyone know?
It will be easier to use document.createElement
and then addEventListener
:
const generateBtns = (item) => {
let paginationBtnsDiv = document.querySelector(".paginationBtnsDiv");
paginationBtnsDiv.innerHTML = "";
for (let num = 1; num <= pageCount; num++) {
let button = document.createElement("button");
button.textContent = num;
button.className = "pages";
button.addEventListener("click", () => onPageClick(item));
paginationBtnsDiv.appendChild(button);
}
};
Related Topics
How to Pass Variables and Data from PHP to JavaScript
How to Save an Html5 Canvas as an Image on a Server
Load More Posts Ajax Button in Wordpress
What Are Cookies and Sessions, and How Do They Relate to Each Other
Angularjs Post Fails: Response For Preflight Has Invalid Http Status Code 404
How to Show Progress Bar While Loading, Using Ajax
Encryption in JavaScript and Decryption With PHP
Send Formdata and String Data Together Through Jquery Ajax
How to Use a C++ Library from Node.Js
How to Modify the Url Without Reloading the Page
Detecting If a Browser Is Using Private Browsing Mode
How to Get the Dom Element Which Contains the Current Selection
Get the Position of a Div/Span Tag
Html/JavaScript: How to Access Json Data Loaded in a Script Tag With Src Set