Skip to content

Commit

Permalink
Javascript function to cut from a element
Browse files Browse the repository at this point in the history
  • Loading branch information
assisfery committed Feb 10, 2020
1 parent 03fa577 commit 8df6e21
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,13 @@ CopyPasteJS.pasteTo("#txtPaste4", function(){
alert("I fell amazing");
});
```

##### Cut Value From Element in JavaScript
```js
CopyPasteJS.cutFrom("#txtCut2");

// OR CALL A FUNCTION
CopyPasteJS.cutFrom("#txtCut2", function(){
alert("I fell amazing");
});
```
22 changes: 22 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ <h5 class="text-success">Paste To Element in JavaScript</h5>

</script>

<br><br>
<h5 class="text-success">Cut Value From Element in JavaScript</h5>
<pre class="alert alert-secondary">CopyPasteJS.cutFrom("#txtCut2");

// OR CALL A FUNCTION
CopyPasteJS.cutFrom("#txtCut2", function(){
alert("I fell amazing");
});
</pre>

<input type="text" class="form-control" value="Cut something here" id="txtCut2">
<br>
<button class="btn btn-success" onclick="cutFromElement()">Cut Value From Element</button>

<script>

function cutFromElement()
{
CopyPasteJS.cutFrom("#txtCut2");
}

</script>

<br>
<div class="text-right text-muted">
Expand Down
22 changes: 22 additions & 0 deletions src/CopyPasteJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ CopyPasteJS.copyFrom = function(elem, callback)
}
}

// COPY FROM ELEMENT
CopyPasteJS.cutFrom = function(elem, callback)
{
var element = document.querySelector(elem);

if(element.tagName && (element.tagName.toLowerCase() == "input" || element.tagName.toLowerCase() == "textarea"))
{
element.select();
CopyPasteJS.data = element.value;
}

document.execCommand('cut');

if(callback)
{
if(typeof callback == "string")
eval(callback);
else
callback();
}
}

// PASTE TO ELEMENT
CopyPasteJS.pasteTo = function(elem, callback)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CopyPasteJS.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8df6e21

Please sign in to comment.