Smile
Smile

Reputation: 2768

What is an alternative for .select() of javascript?

I have following code to select an element (which will be created dynamically):

document.all['ele' + count].select()

Above code is perfectly working in Google Chrome but gives an error in all versions of Internet Explorer:

SCRIPT16389: Incorrect function.

What could an alternative for .select() that can work in both Google Chrome and IE?

Edit:

I will have following Textarea for above function to select

<textarea name="ele[5][t]" id="ele5"></textarea>

Upvotes: 2

Views: 2903

Answers (5)

The Alpha
The Alpha

Reputation: 146239

This is your element

<textarea name="newele[5][t]" id="t_newele5"></textarea>

To select it using jQuery, you may use

$('#t_newele5').action(...); // selects the element ussing it's "id"

Here action is pseudo command to denonstrate the process of calling a method on an element. For example, to call css() method on selected textarea you can write this

$('#t_newele5').css('color', 'red');

Remember to keep your jQuery code inside ready event handler, like

$(document).ready(function(){
    $('#t_newele5').css('color', 'red');
    // more code
});

An Example Here.

Using vanilla JavaScript you can do it like

document.getElementById('t_newele5').style.color = 'green';

An Example Here.

Upvotes: 0

azz
azz

Reputation: 5940

select() is probably not the problem. It's almost certainly the use of document.all.

Javascript solution:

document.getElementById("ele" + count).select();

jQuery solution:

$('#ele' + count)[0].select();

Both are equivalent.

EDIT

IE seems to have a bug regarding hidden inputs (source), try using jQuery to get around it:

$('#ele' + count).select();

Or:

$('#ele' + count).focus();

Upvotes: 2

kylehyde215
kylehyde215

Reputation: 1256

Or even document.querySelector or document.querySelectorAll

Upvotes: 0

Julian
Julian

Reputation: 1491

why don't you use document.getElementById? document.all is not longer supported as you can read here.

Upvotes: 0

Dan Goodspeed
Dan Goodspeed

Reputation: 3590

Are you trying to bind a jQuery function to a regular javascript object? Have you tried doing it this way instead?

$("#ele"+count).select();

?

Upvotes: 0

Related Questions