tags:

views:

76

answers:

3

Hello,

my html is like this

<td>
  <input type="text" class="textinput" id="file_descr_0" name="file_descr[0]" value='' />
  <div id="mean" onClick="removeElement('mean');">{L_FileDescr}</div>
</td>

and to remove the div i am using this function

function removeElement(div){ 
    var d2 = document.getElementById(div); 
    var d1 = d2.parentNode;
    d1.removeChild(d2); 
}

I wanted to know how I can make this removing element dynamic because there are lot of divs which are to be removed and because of this function i will have to give each one a id.

and also after removing how can i focus the input field.

Thank You.

A: 

you could pass the event in and use event.target

function removeElement(event) {
    //...
}

the onclick would just be onclick="removeElement"

Jimmeh
How can i focus the input field next to it?
Shishant
+4  A: 
<div onClick="removeElement(this);">{L_FileDescr}</div>

function removeElement(d2){ 
    var d1 = d2.parentNode;
    d1.removeChild(d2); 

    // edit. also:
    d1.nextSibling.firstChild.focus();
}
David Hedlund
How can i focus the input field next to it?
Shishant
I am getting an error `d1.nextSibling is not a function`
Shishant
Additionally, to focus on the input element, just use d1.firstChild.focus().
MillsJROSS
Same type of error `... is not a function`
Shishant
yes, my bad. it's not a function at all, so remove the parenthesis: `d1.nextSibling.firstChild.focus()`
David Hedlund
`d1.getElementsByTagName('input')[0].focus();`
Shishant
Here, isn't d1 the parent of both the div and the input? If so, then how does d1.nextSibling.firstChild give use access to the input?
ntownsend
`d1` is the `td`, since he's talking about the *next* input, i assumed that there'll be a subsequent `td`, with an `input` in it. so `nextSibling` would get to the `td` after `d1`, and `firstChild` would find the `input` in *that*
David Hedlund
Shisant wasn't asking to focus the next input but the input next to the div.
ntownsend
whoa. i totally didn't notice :D good catch
David Hedlund
A: 

If the input is always the element before the div, you could do something like this

<input type="text" />
<div onClick="removeElementAndFocusInput(this);">stuff</div>

function removeElementAndFocusInput(elem){
    var toRemove = elem;

    toRemove.previousSibling.focus() // set focus to the input

    var parent = toRemove.parentNode;
    parent.removeChild(toRemove); // remove the div
}
ntownsend