NoNameZ
NoNameZ

Reputation: 795

Remove from ... to

So I have a kind of problem: I need to remove "information" from tag to tag including text between it.

So somewhere in html is:

<input type="radio" rel="1" value="Surname1" name="lastnames">Surname1<br>
<input type="radio" rel="2" value="Name2" name="lastnames">Name2<br>
<input type="radio" rel="3" value="lol" name="lastnames">lol<br>
<input type="radio" rel="4" value="lol2" name="lastnames">lol2<br>

And for example:

var current_id = $('input:checked:radio').attr('rel');
$.get('index.php?delete_by_id='+current_id);
$("input:radio[rel='"+current_id+"']").remove();

So, after the GET to php is sent, I need to delete from the radio list deleted item. With input all ok, but with name next to him I have problems...

PS. I know, that situation is stupid, but I don't have any time to rewrite it until tomorrow (exams)

Upvotes: 0

Views: 179

Answers (3)

aaberg
aaberg

Reputation: 2273

I ussually always use a label, to show text that is connected to a radio button:

<input id="radio1" type="radio" rel="1" value="Surname1" name="lastnames"><label for="radio1">Surname1</label><br>
<input id="radio2" type="radio" rel="2" value="Name2" name="lastnames"><label for="radio2>Name2</label><br>
<input id="radio3" type="radio" rel="3" value="lol" name="lastnames"><label for="radio3">lol</label><br>
<input id="radio4" type="radio" rel="4" value="lol2" name="lastnames"><label for="radio4">lol2</label><br>

Then you can do like this in your javascript code:

var current_rel = $('input:checked:radio').attr('rel'),
    current_id = $('input:checked:radio').attr('id');
$.get('index.php?delete_by_id='+current_rel);
$('#' + current_id + ', label[for="' + current_id + '"]).remove();

Upvotes: 0

Paolo Bergantino
Paolo Bergantino

Reputation: 488694

Not sure exactly what you are trying to do and/or why it is not working, but consider writing the code like this:

var $selected_radio = $('input:radio:checked'); // store element reference
$.get('index.php?delete_by_id=' + $selected_radio.attr('rel'), function() {
    // use a callback function to only remove if code succeeds
    $selected_radio.remove();
});

EDIT: Looking back I see you want to remove the input along with its label. You just need to fix your HTML to be what it actually should be:

<label>
    <input type="radio" rel="1" value="Surname1" name="lastnames"> Surname1
</label>

Which then makes the remove code in jQuery look like:

$selected_radio.closest('label').remove();

Upvotes: 1

Shyju
Shyju

Reputation: 218872

Wrap the label with a span element.

<input type="radio" rel="1" value="Surname1" name="lastnames"><span>Surname1</span><br>
<input type="radio" rel="2" value="Name2" name="lastnames"><span>Name2</span><br>

Then while deleting, Get the next span and remove it as well

var item=$('input:checked:radio');

var current_id = item.attr('rel');
$.get('index.php?delete_by_id='+current_id,function(data){
   item.remove();
   item.next("span").remove();
});

It is always a good practice to keep HttpPost Operations for Delete/Update functions. Otherwise anybody can delete a record from your database if they know the item id, by simply executing your page with those querystrings in the browser.

Upvotes: 1

Related Questions