Use Javascript to copy Text from Label - asp.net

Label1 (asp.net control) is located inside Panel1 of my webpage and I have a button called bt. What is the Javascript to copy the Text from Label1 to the clipboard?
#artlung, I placed the below code just outside of my form but inside the body. The last line of code I placed inside Panel1 of my form. Anything wrong with this code because nothing happens when I click the Copy to Clipboard button.
<script language="JavaScript">
var clip = new ZeroClipboard.Client();
clip.addEventListener( 'mouseDown', function(client) {
// set text to copy here
clip.setText( document.getElementById('form1.Label1').value );
// alert("mouse down");
} );
clip.glue( 'd_clip_button' );
</script>
The next line of code is above the script tags but inside Panel1 in my form
<div id="d_clip_button">Copy To Clipboard</div>

I achieved this with javascript. I picked up on the fact that javascript was renaming the label from Label1 to MainContent_Label1. Then I also through debugging saw that what I wanted to grab from the variable was the innerText property. Problem Solved!
I also had the function test for an empty string and then return a confirmation alert to the user when they either copied successfully to the clipboard or when the label was empty with nothing to copy. This is all client-side.
<asp:Label ID="Label1" runat="server" Text="Copy This!!!"></asp:Label>
<script type="text/javascript">
function ClipBoardTest() {
var txt = document.getElementById('MainContent_Label1');
window.clipboardData.setData("Text", txt.innerText);
if(!txt){
alert("Nothing to Copy");
}
else {
alert("Copy to ClipBoard Successful!");
}
}
</script>
<input type="button" id='bt' onclick="ClipBoardTest();" value="Copy" />

Use the zeroclipboard library.

There is also a way to copy text from label without using any external libraries.
copyStrFunction = (copyStr) => {
const el = document.createElement('textarea'); // Create a <textarea> element
el.value = copyStr; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
el.select(); // Select the <textarea> content
document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
document.body.removeChild(el); // Remove the <textarea> element
}
Call the copyStrFunction() onclick of any button and the string.
You can also modify the above function to find the label by Id and then copying the text from it.

Related

KeyUp event with Html.TextBoxFor

I need help in using keyup with #Html.TextBoxFor. I have a html code which creates a new row of texboxes on each button click. Now i want to convert the text entered into that textbox into upper case while the user enters. Thought of using OnKeyUp event, but need help in syntax with #Html.TextBoxFor . I'm new to .Net. Here's my html code line
<div>#Html.TextBoxFor(x =>x.ItemName[0].SerialNumber, new {autocomplete = "off"}) </div>
Help me with the JS also please
You can add OnKeyUp attribute to your textbox:
#Html.TextBoxFor(x =>x.ItemName[0].SerialNumber, new {autocomplete = "off", onkeyup="ToUpper(this);"})
On the script side, you need to have ToUpper method also like this:
<script>
function ToUpper(obj)
{
if (obj.value!="")
{
obj.value = obj.value.toUpperCase();
}
}
</script>

Ipad KeyPad not displaying for the textbox that is placed inside jquery draggable div

I have a application that has jquery draggable div in it.
I have placed four text fields inside the draggable div to get input from user.
Aspx:
<div id="divAdd" runat="server">
<input id="txtCode" placeholder="Location Code" maxlength="20"
type="text" runat="server" />
<input id="txtName" placeholder="Location Code" maxlength="20"
type="text" runat="server" />
...
</div>
<div>
Javascript:
$("#divAdd").draggable({ cursor: 'move', containment: '#divmap',
drag: function () {
fnHandleMove();
}
});
I cant place the cursor in those textfield in IPAD by tapping on it. However Desktop version works fine.
If i comment out that javascript part, i am able to place the cursor and keypad shows.
Is this bug with jquery UI-draggable or i am doing anything wrong?
Apart from JqueryUI.js i use JqueryTouchPunch.js and JSPlumb.js in the application.
Any help will be appreciated.
OK here's a solution if your textfield whatever HTML element is, isn't focusing,scrolling, selecting words, moving text cursor around the text and whatever different scenarios might come then you may override the jquery.ui.touch.punch.js script. I assume that your element isn't the draggable one but probably a child of it as my case was.
Put a class on your html element, for example class="useDefault".
Then go to the script file and find that part:
...
function simulateMouseEvent (event, simulatedType) {
// Ignore multi-touch events
if (event.originalEvent.touches.length > 1) {
return;
}
event.preventDefault();
var touch = event.originalEvent.changedTouches[0],
simulatedEvent = document.createEvent('MouseEvents');
....
As you can probably see event.preventDefault(); assures that jquery.ui.touch.punch.js
overrides the default behaviors of the browser. To prevent that for our particular class node, make the following modifications:
if (event.originalEvent.touches.length > 1) {
return;
}
var touch = event.originalEvent.changedTouches[0],
simulatedEvent = document.createEvent('MouseEvents');
//As you can see here is your class element check
if (touch.target.className === "useDefault") {
event.stopPropagation();
} else {
event.preventDefault();
}
This solution is tested with webkit browsers only and jQuery UI Touch Punch 0.2.2 release.
Hope that quick solution helps, BR
A variation from kidwon idea:
In the same method, same part:
function simulateMouseEvent (event, simulatedType) {
// Ignore multi-touch events
if (event.originalEvent.touches.length > 1) {
return;
}
event.preventDefault();
replace with:
function simulateMouseEvent (event, simulatedType) {
// Ignore multi-touch events
if (event.originalEvent.touches.length > 1) return;
if (simulatedType == 'mouseup') {
var elem = $(event.originalEvent.changedTouches[0].target);
if (elem.is("input") || elem.is("textarea")) elem.focus();
else event.preventDefault();
} else event.preventDefault();
Hope it helps.

Calling my own function during with onClick

How do I call a user-define function such as this one using the onClick attribute of a input button? More specifically what special steps must I take in JQuery and what would the HTML markup look like? Thanks
function simClick(keyCode) {
var e = jQuery.Event("keypress");
e.keyCode = 8;
$(document).trigger(e);
}
<input type="button" ID="delBtn" class="calcBtn" value="Del" onclick="???????" />
HTML
<input type="button" ID="delBtn" class="calcBtn" value="Del" />
Javascript in separate file
// When the DOM is ready
$(function() {
// Function that is executed w keypress or button click
doThis = function() {
// Stuff to do
}
// To do when element with ID delBtn is clicked
$("#delBtn").click(function() {
// Stuff to do when input is clicked
doThis();
});
// To do when key is pressed
$(document).keydown(function(event) {
// Stuff to do when key is pressed
// Can check which key was pressed here.
var code = (event.keyCode ? event.keyCode : event.which);
if(code == 8) { //Enter keycode
doThis();
}
});
});
There are many ways to attach a handler to when that button is clicked. Take a look at jQuery selectors.
You could also use the attribute equals selector
$("input[value='Del']")...... // For the input with a value of Del
I'm not sure what you quoted JS has to do with the input button, since it looks like you're trying to work with a keypress instead of a click in that function.... But the above jQuery is how you capture a click on that input button.
Take a look at, "Which key was pressed?"

asp.net external JavaScript file doesn't find Control.ClientID

On load I'm both calling a JavaScript setTimeout() function that will hide a .NET Panel control, and hiding it in the code behind on first load. Clicking the save button will set the Panel to visible then reload the page at which point a setTimeout() function is called... so basically you click save, and see a panel with "Details Saved" for three seconds, at which point it disappears.
The problem is the external JavaScript file can't find _pDivAlert.ClientID (I've debugged and it returns null). It only works when the code is in a tag in the .aspx page. Any suggestions as to how I can either pass the client ID to the HideControl() function or find the ClientID from the external JS file?
Here's my code, any suggestions?
<script language="javascript" src="Forms.js" type="text/javascript"></script>
<body onload="ToggleAlert()">
<form id="form1" runat="server">
<script type="text/javascript">
//alert the user that the details were saved
function HideControl() {
var control = document.getElementById('<%=_pDivAlert.ClientID %>');
if(control != null)
control.style.display = 'none';
}
function ToggleAlert() {
setTimeout("HideControl()", 3000);
}
</script>
I've also tried sending the ClientID within the ToggleAlert() call, but that didn't work:
<body onload="ToggleAlert('<%=_pDivAlert.ClientID %>')">
External JS:
function HideControl(_c) {
var control = _c;
if (control != null)
control.style.display = 'none';
}
function ToggleAlert(_c) {
setTimeout("HideControl(_c)", 3000);
}
can you show your markup with the panel and the codebehind where you hide it?
there's a difference between setting the Visible property to false and setting the style display attribute to none- the first will not render the element at all, meaning there isn't anything rendered with the id you're looking for.
edit: it's probably because of the way you're calling HideControl in the timeout- this should be a function instead of a string.
try doing
function ToggleAlert(_c) {
setTimeout(
function () {
HideControl(_c);
}, 3000);
}
just for clarity, when you pass a string to setTimeout, it's evaluated and then run. the code chunk that eval produces will run in a different scope than your ToggleAlert method, and so _c won't be available at that time.
edit: you also need to actually get a reference to the control. you're passing the id string to ToggleAlert, which relays it to HideControl, which is expecting an object not a string.
function HideControl(_c) { // _c is the id of the element
var control = document.getElementById(_c);
if (control != null)
control.style.display = 'none';
}

how to enable an css property for the ajax tab panel using javascript

i am using asp.net ajax tab container[ which has 2 tab panel]
under each tab panel i have an div tag. now by default.i have my Activetabindex="0"
now i need to enable css property for the div tag using javscript so that there is no post back happening. i doing like this css property for the tab panel 1 is not getting applied
this is my script what i doing. if i do the same thing in code behind for the ta selected index change it works. but thatcause an post back.
now i need t o do it my javscript only
OnClientActiveTabChanged="PanelClick"
<script type="text/javascript" language="javascript">
function PanelClick(Sender, e) {
debugger;
var CurrentTab = $find('<%=Tab1.ClientID%>');
if( Sender._activeTabIndex==0) {
debugger
document.getElementById('<%=mycustomscroll2.ClientID%>').className = '';
document.getElementById('<%=mycustomscroll2.ClientID%>').Enabled = false;
document.getElementById('<%=mycustomscroll.ClientID%>').className = 'flexcroll';
}
if (Sender._activeTabIndex == 1) {
debugger
document.getElementById('<%=mycustomscroll.ClientID%>').className = '';
document.getElementById('<%=mycustomscroll.ClientID%>').Enabled= false ;
document.getElementById('<%=mycustomscroll2.ClientID%>').className = 'flexcroll';
}
}
</script>
so how to i enable my css property for the div using javascript for the tab panel
anyhelp would be great
thank you
Here is a javascript function which will sort of do what you want:
function PanelClick(Sender, e) {
var scroll1 = $get('<%=mycustomscroll.ClientID%>');
var scroll2 = $get('<%=mycustomscroll2.ClientID%>');
if(Sender._activeTabIndex == 0) {
scroll1.setAttribute('class', 'flexcroll');
scroll2.setAttribute('class', '');
} else if(Sender._activeTabIndex == 1) {
scroll1.setAttribute('class', '');
scroll2.setAttribute('class', 'flexcroll');
}
}
There really is no such thing as "enabled" in HTML and JavaScript. HTML has a "disabled" attribute, but it only applies to these elements: button, input, optgroup, option, select and textarea. It is used like so:
<input type="text" name="txtSomething" id="txtSomething" disabled="disabled">
and in JavaScript, similar to setting the class attribute, above:
$get('txtSomething').setAttribute('disabled','disabled'); // disable the input
$get('txtSomething').setAttribute('disabled',''); // enable the input
But this will not work for other elements like <div> and <span> tags.

Resources