haii friends....
i want to select a particular string from textarea...i need it in javascript....
am using the tag <layout:textarea>
i did not need the whole ,but only a part...plzz help me...

haii friends....
i want to select a particular string from textarea...i need it in javascript....
am using the tag <layout:textarea>
i did not need the whole ,but only a part...plzz help me...

What markup language are you using?

In HTML a text area is just <textarea>.

You'll need a reference to the DOM node representing the text area, example:

textarea HTML

<textarea id="mytext">
</textarea>

select whats inside the textarea with JS

// get the textarea by its id
var txt = document.getElementById('mytext');
// theres the contents inside the textarea
var value = txt.value;

btw: once you have the value of the string, you need to do some matching with regex or similar to get what you want out of the string.

haii friends....
i want to select a particular string from textarea...i need it in javascript....
am using the tag <layout:textarea>
i did not need the whole ,but only a part...plzz help me...

thankzzz....
am using struts framework and <layout:textarea> is provided in it..
it has no property like "id" (ie..<textarea id="">)....
its syntax is like this..
<layout:textarea property="' key=""></layout:textarea>

"property" is used to represent the textarea ....

and wht is ment by "getElementById"..plzzz help

thankzzz....
am using struts framework and <layout:textarea> is provided in it..
it has no property like "id" (ie..<textarea id="">)....
its syntax is like this..
<layout:textarea property="' key=""></layout:textarea>

"property" is used to represent the textarea ....

and wht is ment by "getElementById"..plzzz help

You'll need to find out how to render HTML attributes using struts.

-----

Re: getElementById

Once the HTML is rendered on the page, the browser will create a DOM (Document Object Model) representation of the HTML for programmatic access from JavaScript.

The DOM is a W3C specification that models XML data as Objects. It is quite simple. Take for example some XML:

<root>
  <child1 id="child1_id">Im a child of root</child1>
  <child2 id="child2_id">
    <subchild1 id="subchild1_id">Im a child of child2</subchild1>
  </child2>
</root>

There are many ways to model this XML document. The model used in browsers is the DOM. So in JavaScript, if you have a reference to the XML Document you could traverse it and select elements (DOM nodes) as well as manipulate it.

If the XML document reference is: XMLDoc, then the root would be:

XMLDoc.documentElement;

<child1> would be:

XMLDoc.documentElement.firstChild

or

XMLDoc.documentElement.childNodes.item(0);

or

XMLDoc.documentElement.getElementsByTagName('child1').item(0);

or

XMLDoc.documentElement.getElementsById('child1_id');

(These are all example of DOM methods for selecting Nodes/Elements).

getElementsById('child1_id') selects the child Node/Element with the id attribute 'child1_id'.

In HTML, the DOM is already created by the browser and available through document object. So you can use document to select any HTML element using DOM methods.

document.getElementById('any_id_in_the_html_document');

kk thankzzz.....
is there any method in java like "selectionStart"

kk thankzzz.....
is there any method in java like "selectionStart"

Do you want to get the Text that the user has currently selected?

Do you want to get the Text that the user has currently selected?

yes......i need that....

You are welcome community of scripters :)

function get_selection(the_id)
{
    var e = document.getElementById(the_id);
    
    //Mozilla and DOM 3.0
    if('selectionStart' in e)
    {
        var l = e.selectionEnd - e.selectionStart;
        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var r = document.selection.createRange();
        var tr = e.createTextRange();
        var tr2 = tr.duplicate();
        tr2.moveToBookmark(r.getBookmark());
        tr.setEndPoint('EndToStart',tr2);
        if (r == null || tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: '' };
        var text_part = r.text.replace(/[\r\n]/g,'.'); //for some reason IE doesn't always count the \n and \r in the length
        var text_whole = e.value.replace(/[\r\n]/g,'.');
        var the_start = text_whole.indexOf(text_part,tr.text.length);
        return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };
    }
    //Browser not supported
    else return { start: e.value.length, end: e.value.length, length: 0, text: '' };
}

function replace_selection(the_id,replace_str)
{
    var e = document.getElementById(the_id);
    selection = get_selection(the_id);
    var start_pos = selection.start;
    var end_pos = start_pos + replace_str.length;
    e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);
    set_selection(the_id,start_pos,end_pos);
    return {start: start_pos, end: end_pos, length: replace_str.length, text: replace_str};
}

function set_selection(the_id,start_pos,end_pos)
{
    var e = document.getElementById(the_id);
    
    //Mozilla and DOM 3.0
    if('selectionStart' in e)
    {
        e.focus();
        e.selectionStart = start_pos;
        e.selectionEnd = end_pos;
    }
    //IE
    else if(document.selection)
    {
        e.focus();
        var tr = e.createTextRange();
        
        //Fix IE from counting the newline characters as two seperate characters
        var stop_it = start_pos;
        for (i=0; i < stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) start_pos = start_pos - .5;
        stop_it = end_pos;
        for (i=0; i < stop_it; i++) if( e.value[i].search(/[\r\n]/) != -1 ) end_pos = end_pos - .5;
        
        tr.moveEnd('textedit',-1);
        tr.moveStart('character',start_pos);
        tr.moveEnd('character',end_pos - start_pos);
        tr.select();
    }
    return get_selection(the_id);
}

function wrap_selection(the_id, left_str, right_str, sel_offset, sel_length)
{
    var the_sel_text = get_selection(the_id).text;
    var selection =  replace_selection(the_id, left_str + the_sel_text + right_str );
    if(sel_offset !== undefined && sel_length !== undefined) selection = set_selection(the_id, selection.start +  sel_offset, selection.start +  sel_offset + sel_length);
    else if(the_sel_text == '') selection = set_selection(the_id, selection.start + left_str.length, selection.start + left_str.length);
    return selection;
}

Edited 6 Years Ago by Ezzaral: Fixed code tags.

Before your contribution this thread was 2 1/2 years dead.
In the meantime tight code for this function has been posted several times.

This question has already been answered. Start a new discussion instead.