oussama kamal oussama kamal - 6 months ago 17
jQuery Question

Remove text between two html tags using jQuery

I have the following HTML :

<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .
<label for="element_18_1">Dollars</label>
</span>


I would like to remove the dot between the input and the label tags using jQuery.

I tried this code with no result :
$('#element_18_1').contents(':gt(2)').remove();
Thanks

Answer

This removes all texts which are not inside a tag (as element)

$(document).ready(function(){
current = $('#element_18_1');
siblings = $('#element_18_1').siblings();
parent = $('#element_18_1').parent();
$(parent).html('');
$(parent).append(current).append(siblings);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<span>
<input type="text" value="0" size="10" class="element text currency" readonly="readonly" name="element_18_1" id="element_18_1"> .       
<label for="element_18_1">Dollars</label>
</span>

You can make it shorter by joining some lines. I tried to show it step by step

Comments