I am unable to delete a div with an id with a period or an asterix.
<div id="*xxx."></div>
<div id=".xxx*"></div>
I have the jquery code, which deletes the <div id="xxx"></div> but not the above. Not looking for the jQuery code, but does it need a forward slash?
—-req by anurag——–
// JavaScript
$('.s').click(function (e) {
var wd_del = (e.target.id);
var yy = wd_del.substr(1, 100);
$(document.getElementById("" + yy)).remove();
$(document.getElementById("s" + yy)).remove();
});
// HTML
<div id="s_t">
<? do { $w1=$ row_ws1[ 'wd']; ?>
<div id="<? echo $w1 ?>" class="ul_shh" style="cursor:pointer;">
<span id="s<? echo $w1 ?>" class="s">
x
</span>
<? echo $w1; ?>
</div>
<? } while ($row_ws1=m ysql_fetch_assoc($ws1)); ?>
</div>
Thanks
Jean
Alternate Approach
I think you can do without messing around with any id’s at all. Rely on the document structure instead to provide the information. It looks like you have a list of divs, where each div can be deleted by clicking on an
xthat is present inside the div.So if you had a structure like:
Assign the delete event to all spans as,
That should free you from having to rely on the id at all, as long as you stick to a basic structure of putting the span inside the div and assign appropriate class names. If you want to know the ID of the clicked element’s parent, store it as a data attribute instead of an id. That keeps jQuery and older browsers happy. For example,
which can be retrieved inside the span click handler, as:
Old Approach
Query using the native
getElementByIdmethod to ensure that the element gets selected if the user-agent considers that to be a valid ID. jQuery will do some extra processing on the passed in ID, so it’s better to query natively:Or escape the characters
*and.with\\. Here’s a jQuery-esque solutionWorks on all moojor browsers for me. Do IE tests yourself 🙂 See this example.
Note, that the restrictions for what constitutes a valid ID string as per HTML5 are:
Quoting from the spec: