I have this code:
<script>
function del(a){
alert(a);
alert($("#"+a).html());
alert($("#td").html());
alert($("#div").html());
}
</script>
<td id="td">wtf</td>
<div id="div">wtf</div>
<table>
<tr>
<td id="bbbbb">test</td><td><span onclick="del('bbbb');">click</span></td>
</tr>
</table>
Alerts are
bbbbb
test
null
wtf
td content is marked as null (ignored) ?
Why?
proof .php
UPDATE:
look here and my alert gives me null, too
I have this code:
<script>
function del(a){
alert(a);
alert($("#"+a).html());
alert($("#td").html());
alert($("#div").html());
}
</script>
<td id="td">wtf</td>
<div id="div">wtf</div>
<table>
<tr>
<td id="bbbbb">test</td><td><span onclick="del('bbbb');">click</span></td>
</tr>
</table>
Alerts are
bbbbb
test
null
wtf
td content is marked as null (ignored) ?
Why?
proof http://sandbox.phpcode.eu/g/743de.php
UPDATE:
look here and my alert gives me null, too
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 27, 2011 at 18:42 genesisgenesis 51k20 gold badges98 silver badges126 bronze badges 1- 4 Please note that JavaScript DOM manipulation on invalid HTML is not predictable. You can never guess what DOM tree the browser will build. – Álvaro González Commented Jul 27, 2011 at 18:48
7 Answers
Reset to default 5First off, you cannot have <td>
elements outside of a table, but it seems in your update, you've fixed that.
In your latest update, the fiddle you posted returns null because of how jQuery selectors work. The ID you are looking for is "5c192.php", so when you do $('#'+a)
it becomes $('#5c192.php')
, which is being interpreted as 'ID=5c192' and 'class=php', because .
is a class selector.
Change $('#'+a)
to$('[id="'+a+'"]')
.
alert($("#"+a).html());
is null because you are passing an invalid id.
alert($("#td").html());
is null because the element td is not valid html in its context.
Edit: Your code works fine there. Just your id is not valid this time. Can't start with numeric.
Should enclose TDs in TABLE tags.
Like this example: jsfiddle
You're calling del("bbbb")
but the id of the <td>
is "bbbbb", with five b's. Not sure if this is your issue though. Also, you can't have a <td>
outside of a table, which is probably why the $("#td")
is messing up. Try:;
<script>
function del(a){
alert(a);
alert($("#"+a).html());
alert($("#td").html());
alert($("#div").html());
}
</script>
<div id="div">wtf</div>
<table>
<tr>
<td id="bbbbb">test</td><td><span onclick="del('bbbbb');">click</span></td>
<td id="td">wtf</td>
</tr>
</table>
Edit
a td
is not valid outside of a table. If you look at the generated output via the web inspector you'll see that the td has been stripped out and it's content placed on the page as plain text. The td is invalid there and thus trying to get to it via JS is going to prove difficult and not a good idea.
That floating <td>
element is not valid and basically gets parsed out into
wtf
without the invalid td elements. Change it to a span, div, etc. it will work. In addition to the missing b
UPDATE:
In your jsFiddle you have an invalid ID of 5c192.php
. Specifically, not starting with a letter. From What are valid values for the id attribute in HTML?
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Although, I would remove the period as well.
The first td (the one with the id="td") is outside of a table (tds are always supposed to be inside a table) and you call the function del giving a string with 4 b, bbbb while your td has 5 d, ddddd.
Since bbbbb (5x) and bbbb(4x) are different.