How do you add some content in a specific place in Javascript at the user's bequest? I'm writing a sort of CSS designer with the idea that it shows the user what the style looks like by writing it to a file and then loading it (using PHP). The thing is I want it to actually be useful, so it needs to be able to have as many elements and properties as the user wants. I know that trick where you have an empty <div> and set its innerHTML member but that will leave me a set number, and I don't want to just stick billions and billions of <div> tags inside my code (its already messy enough).
tl; dr how do I dynamically instert code into a webpage at a set position but allowing an arbitrary number of insertions?
>>2
I also know x86 assembly, C, C++, C#, Python and Perl. I felt like fucking about with web development, since I'm pretty poor at it. Usually, I'm more interested in applications programming... In fact, this is the first real web development I've ever done.
Oh and I'd be willing to bet the reason you say "web development is for chumps" is because you tried it once and sucked at it (probably tried to make a website, but it ended up looking like dogshit (or a virtual terminal because you're so original and the first person to make a website look like a tty) because you're an aspie and aspies cannot into design).
>>3
I've been toying around with Haskell. It's confusing for me but I'll get it.
Name:
Anonymous2011-08-19 15:32
I don't really understand your question.
If you're working in Javascript then in all likelihood you have direct access to a DOM API already, so you don't have to go through any middlemen. You'd just need to serialize the current state of the DOM, or use some kind of ``Unit of Work'' implementation, if you want to store/retrieve user changes.
The boxed-in things are text entries and the underlined things are URLs (which are just <a href="#" onClick="someFunction()">)
When the user clicks "Add property" I want it to insert "| Property | : | Value | ;". I can do that fine by putting a <div> there and changing it's innerHTML member, but that means limiting the number of properties that the user can have and I want it to be limited by the hardware, OS or browser, but not by my HTML code.
Is this how you do it? var div = document.createElement("div");
div.innerHTML = document.getElementById("ee2e7a91").innerHTML;
document.body.appendChild(div);
then it should be working. Have you tried looking at the error console to see if the script is running into an error and stopping before that gets executed? On firefox, it's under Tools->Error Console. If not that, then are you sure the code is being executed? You can stick an alert('qweweofijwef'); to verify that.
Name:
Anonymous2011-08-19 18:18
>>17
I have looked at the error console, no errors were being reported.
function add_property()
{
alert("Add property");
var div = document.createElement("div");
div.innerHTML = document.getElementById("ee2e7a91").innerHTML;
document.body.appendChild(div);
}
The alert is displayed but nothing else happens (the page source when viewed from Firefox stays the same (yes, I re-opened it, I know it doesn't refresh automatically)).
Name:
Anonymous2011-08-19 18:21
>>15 jquery custom templating function with tiny html templates stored in <script type="text/template"> tags for more involved dynamic stuff
Why the fuck do people love overcomplicating things?
Get the Node of the element you want to add into with getElementById or whatever suitable. appendChild() your new element.
Simple as that.
Firefox has a DOM Inspector you can use to see what it's doing.
I wrote this in 2 minutes. Probably not valid HTML nor JS but it works and gets the point across. <html>
<body>
<script type="text/javascript">
function add_property() {
var d = document.createElement("div");
d.innerHTML = "Hello world!";
document.getElementById("test").appendChild(d);
}
</script>
<div id="test">
This is the first line.
</div>
<input type="button" onclick="add_property()" value="add div">
</body>
</html>
Works in everything from IE6 to FF4 (tested).
that's very strange. I would try checking the value of that getElemntById. If the right hand side is in fact a non empty sting, then you should see something at the bottom of the page after that executes.
javascript:div=document.createElement('div');div.innerHTML='<i>hai thar!</i>';document.body.appendChild(div);void(0);
please copy and paste the above into your browser url bar, to see the effect!
it's better to store large chunks of html as html than as javascript code
Name:
Anonymous2011-08-19 19:25
>>21 function add_property()
{
alert("Add property");
var div = document.createElement("div");
div.innerHTML = document.getElementById("ee2e7a91").innerHTML;
if (document.getElementById("ee2e7a91").innerHTML == null)
alert("null");
else
alert("Not null");
document.body.appendChild(div);
}
Well, I tried this code, and the second alert says "Not null" so it is returning an element handle. Your code works, though.
I just tried this: alert(document.getElementById("ee2e7a91").innerHTML);
and it is blank.
That <div> is not blank though: <div id="ee2e7a91">
<tr>
<td></td>
<td>
<select>
<?php
/*
* Generate list of options
*/
$file = fopen("res/properties.lst", "r");
while (!(feof($file))) {
$string = rtrim(fgets($file));
if ($string != "")
echo "<option value=\"$string\">$string</option>\n";
}
fclose($file);
?>
</select>
</td>
<td>:</td>
<td>
<input type="text" />;
</td>
</tr>
</div>
>>20
Thanks, I'll take a look. I'm using FF6 as of today, btw.
you might want to try alert(blahblah.innerHTML) instead of using that if statement, as the empty string might not be null. I can never keep corner cases like this straight.
Name:
Anonymous2011-08-19 19:37
>>24
I tried that as well, it comes out blank.
>I just tried this:
>alert(document.getElementById("ee2e7a91").innerHTML);
>and it is blank.
I ran this: alert(document.getElementById("ee2e7a91").innerHTML);
alert(document.getElementById("dda3fb44").innerHTML);
alert(document.getElementById("f15a1547").innerHTML);
and only the first one came out blank.
The second one is another div tag (I can't remember why, but it contains ee2e7a91 (I tried making ee2e7a91 the child of body instead but it didn't make a difference)) and the second is the <select></select> code I posted above.
Name:
Anonymous2011-08-19 19:42
>>25
I'm going to sleep now, since tomorrow I go on holiday to Spain. I probably won't have Internet access for about a week either, so hopefully this gets resolved in the morning. If not... this is spa/prog/rta, it'll probably still exist in a week.
it sounds like you might have an issue with e script running before the body is done loading. I might be wrong, but you can try moving your script tag to the end of the document. Good luck with everything.
2011
Using innerHTML
I really hope you guys don't do this.
Name:
Anonymous2011-08-20 8:10
>>28
>2011
>saying really instead of seriously I seriously hope you guys don't do this costanza.jpg what is this i don't even I SERIOUSLY HOPE YOU GUYS DON'T DO THIS COST ANZA . JPG I SERIOUSLY HOPE YOU GUYS DON'T DO THIS
>>27
Alright. I'll play around a bit more when I get home.
There are many brands of <a href="http://www.barbourjackets-uk.org/"><strong>barbour fusilier</strong></a> in the market today. Each of the brand promises to bring out something new to the customers.