Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

Need help sa Javascript

yeoh_8

Amateur
Advanced Member
Messages
110
Reaction score
0
Points
26
Need help guys with my code. Sinusubukan ko kase gumawa ng template na web based instead na sa notepad namin ginagamit yung mga information. Hindi ko mapagana yung multiple text input para mag copy to clipboard. Laging yung isang element lang ang na ccopy, yung pangalawa ayaw na.

<input type="text" id="unique" size="9" value ="SESA / D-ID:" readonly/>
<input type="text" id="unique" size="18" value="">
<button id="copybtn" onclick="doCopy()"> Copy to clipboard </button>

<script type="text/javascript">

function doCopy() {
var unique = document.getElementById('unique');
unique.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log("Text Copy Was "+message);

}

catch(err) {

console.log("There was an error copying");
}

}
</script>
 
ang ID should be UNIQUE for each page. so jan sa example mo, yung ID mo na may value na "unique" ay hindi talaga unique, dahil dalawang elements ang may magkaparehong value. So ang tendency ng script mo ay to select the last instance of the said ID, to call several instances, you may use the element type, or, to be specific, select them using classes instead. Now, kung meron ka multiple instances of the said class, kailangan mo na gumamit ng array to hold the values for each element.
 
Need help guys with my code. Sinusubukan ko kase gumawa ng template na web based instead na sa notepad namin ginagamit yung mga information. Hindi ko mapagana yung multiple text input para mag copy to clipboard. Laging yung isang element lang ang na ccopy, yung pangalawa ayaw na.

<input type="text" id="unique" size="9" value ="SESA / D-ID:" readonly/>
<input type="text" id="unique" size="18" value="">
<button id="copybtn" onclick="doCopy()"> Copy to clipboard </button>

<script type="text/javascript">

function doCopy() {
var unique = document.getElementById('unique');
unique.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log("Text Copy Was "+message);

}

catch(err) {

console.log("There was an error copying");
}

}
</script>


========================================================
Try mo ito sa baba!=
========================================================


<input type="text" id="unique" size="9" value ="SESA / D-ID:" readonly/>

<!--ibahin mo ang id na ito, peru kahit di mo sya ibahin magrarun parin naman code mo. Malaki nga lang ang chance na wrong output.-->
<!--ang kinukuha nya ay ang Unang instance ng ID or first index kaya yung pangalawa ay na ignore-->
<!--Nasa baba ang part na mali mo, may comment-->

<input type="text" id="unique" size="18" value="">

<button id="copybtn" onclick="doCopy()"> Copy to clipboard </button>

<script type="text/javascript">
function doCopy()
{
var unique = document.getElementById('unique');
unique.select();
try
{
var successful = document.execCommand('copy');
var msg = (successful) ? 'successful' : 'unsuccessful';

//--Pinalitan ko lang yung "message" ng "msg" kasi undefined sya. Dito ka may mali maliban sa ID na unique!
console.log("Text Copy Was "+msg);
}
catch(e)
{
console.log("There was an error copying");
}
}
</script>



kung mag ka copy ka ng multiple element
gamitan mo ng loop, either via querySelectorAll, elementsTagName, elementsClassName
wag kang gumamit ng id lalo na sa unpredictable id's kasi iba iba yun, dapat mga unique yun, maliban nalang kung ipapasok mo lahat sa array ang ID's tapos iloop mo yun.


var inputElements = document.querySelectorAll('input[type="text"]');

for(var i=0; i < inputElements.length; i++)
{
inputElements.select();
try
{
}
catch(e)
{
}
}
 
Last edited:
Medyo tricky yung problem mo TS, if I'm not mistaken, you want to put the text of two different input into the clipboard at the same time.., my tricks ako para dian, ginawa ko eh.. Ginawa kung class yung two input then niloop ko para i copy ko yung text nila at ilagay sa variable, after nun gumawa ako ng "textarea" element then nilagay ko dun yung value ng ni loop natin, example SESA / D-ID:test123 so yung textarea is <textarea>SESA / D-ID:test123</textarea> after nun select() yung textarea then document.execCommand("copy");
then document.body.removeChild(textarea);

after nun na copy na yung SESA / D-ID: at yung input mo sa clipboard..

ito yung screenshot

View attachment 351194


<body>

<input type="text" class="unique" size="9" value="SESA / D-ID:" readonly/>
<input type="text" class="unique" size="18" value="">
<button id="copybtn" onclick="doCopy()"> Copy to clipboard </button>

<script>

function doCopy() {

try{
var unique = document.querySelectorAll('.unique');
var msg ="";

unique.forEach(function (unique) {
msg+=unique.value;
});

var temp =document.createElement("textarea");
var tempMsg = document.createTextNode(msg);
temp.appendChild(tempMsg);

document.body.appendChild(temp);
temp.select();
document.execCommand("copy");
document.body.removeChild(temp);
console.log("Success!")




}
catch(err) {

console.log("There was an error copying");
}

}
</script>
</body>
 

Attachments

  • test.png
    test.png
    53.3 KB · Views: 11
Medyo tricky yung problem mo TS, if I'm not mistaken, you want to put the text of two different input into the clipboard at the same time.., my tricks ako para dian, ginawa ko eh.. Ginawa kung class yung two input then niloop ko para i copy ko yung text nila at ilagay sa variable, after nun gumawa ako ng "textarea" element then nilagay ko dun yung value ng ni loop natin, example SESA / D-ID:test123 so yung textarea is <textarea>SESA / D-ID:test123</textarea> after nun select() yung textarea then document.execCommand("copy");
then document.body.removeChild(textarea);

after nun na copy na yung SESA / D-ID: at yung input mo sa clipboard..

ito yung screenshot

View attachment 1270219

Maraming salamat sa tulong mo master. Problema ko na lang yung line break pag nagdagdag ako ng element. Ganito yung lumalabas

Enter SESA / D-ID: Test123Enter Name: John Smith

Pinalitan ko na nga ng Textarea ganun pa din po.

<textarea class="unique" size="9" readonly/>Enter SESA / D-ID: </textarea>
<textarea class="unique" size="18"></textarea> <br>
<textarea class="unique" size="9" readonly/>Enter Name: </textarea>
<textarea class="unique" size="18"></textarea> <br>

Sana makita ko kayo ng personal para mapa mirienda man lang, laking tulong ng mga ganitong forum sa mga baguhang nagaaral ng JS kagaya ko.
 
Kung saan ka banda nag cocopy ng value example msg; dagdagan mo lang ng +'\n'
msg += input.value+'\n';

\n "new line" is equivalent to <br/>
Peru ang <br/> hindi gagana sa textarea.
 
Kung saan ka banda nag cocopy ng value example msg; dagdagan mo lang ng +'\n'
msg += input.value+'\n';

\n "new line" is equivalent to <br/>
Peru ang <br/> hindi gagana sa textarea.

Should be "\r\n" to be sure. \r = carriage return.
 
Mga masters, sinubukan ko po yung advice niyo, tried the following:

msg += input.value+'\n';

Enter SESA / D-ID:
SESA12345
Enter Name:
John Smith

msg += input.value+'\r\n';

Enter SESA / D-ID:
SESA12345
Enter Name:
John Smith

Ano po kaya ang gagawin ko para sana maging ganito yung output?

Enter SESA / D-ID: SESA12345
Enter Name: John Smith
 
Last edited:
Sa loob ng loop mo, foreach or forloop


unique.forEach(function (unique)
{


//for the label "Enter SESA / D-ID:"
if(x <= 1){msg += unique.value} //should result to "Enter SESA / D-ID:"

//for the value "SESA12345"
msg += unique.value+'\n\r'; //should be "Enter SESA / D-ID:SESA12345\n\r"

//reset x;

});


The result should be

Enter SESA / D-ID: SESA12345\n\rEnter Name: John Smith\n\r

when printed

Enter SESA / D-ID: SESA12345
Enter Name: John Smith



NOTE: This code was not tested please check if it works
 
Last edited:
Back
Top Bottom