/    Sign up×
Community /Pin to ProfileBookmark

get first line & write to textfield

Hi

I tried to write a piece of code that gets a piece of text that was copied into a textfield, splits it and returns the first line of it into another textfield.
Can you tell me what’s wrong with what I wrote?

[CODE]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<meta http-equiv=”Content-Style-Type” content=”text/css”>
<meta http-equiv=”Content-Script-Type” content=”text/javascript”>

<script type=”text/javascript”>

function getTitle()
{
var form= document.getElementById(‘titleForm’);
var article= document.getelementById(‘articleField’).value;
var title= article.split(“n”)

inp=document.createElement(‘input’);
inp.type=’text’;
inp.name=’titleField’;
inp.id=’titleField’;
inp.value=title[0];
form.appendChild(inp);
}

</script>
</head>
<body>
<form id=”titleForm” action=””>
<input type=”text” name=”articleField” id=”articleField”/>
<INPUT TYPE=”button” NAME=”button” Value=”Click” onClick=”getTitle()”>

</form>
</body>
</html>[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 04.2010 —  gets a piece of text that was copied into a textfield, splits it and returns the first line of it [/QUOTE]
A text in a texfield [I]has a single line[/I]. So, there nothing to be used as a split delimiter. So?
Copy linkTweet thisAlerts:
@mrmajorauthorOct 04.2010 — A text in a texfield [I]has a single line[/I]. So, there nothing to be used as a split delimiter. So?[/QUOTE]

ah, thanks for pointing that out.

I'll try it with a textarea then. Unless you know that's wrong and you have the solution?
Copy linkTweet thisAlerts:
@KorOct 04.2010 — <i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;

&lt;script type="text/javascript"&gt;
function getTitle()
{
var form= document.getElementById('titleForm');
var article= document.getElementById('articleField').value;
var title= (article.split('n')[0]).split('r');// some browsers, or browsers on some OSs, use r as carriage return
var isIE=/*@cc_on!@*/false;//IE detector
var inp;
if(isIE){ // IE hack
inp=document.createElement('&lt;input type="text" name="titleField"&gt;');
}
else{
inp=document.createElement('input');
inp.type='text';
inp.name='titleField';
}
inp.id='titleField';
inp.value=title[0];
form.appendChild(inp);
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="titleForm" action=""&gt;
&lt;textarea name="articleField" id="articleField"&gt;&lt;/textarea&gt;
&lt;input type="button" name="button" value="Click" onclick="getTitle()"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

I have corrected some of your HTML and JavaScript errors. I have inserted also a small hack to bypass a well known IE bug (related to the handle of the name attribute in case of new DOM created elements)
Copy linkTweet thisAlerts:
@mrmajorauthorOct 04.2010 — Thanks again!

I'm glad I was heading in the right direction.

Your corrections really helped me learn to understand.

I guess everyone makes this kind of mistakes in the beginning ?
×

Success!

Help @mrmajor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...