Jump to content
  • Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
paulmo

Form-submitted captions below form-submitted images

Recommended Posts

paulmo    3

Hello, captions need to appear below each form-submitted URL image in tile fashion on the page. I'm trying various things here and it's not happening. Live example here. Thanks in advance to team W3.

<!DOCTYPE html>
<html>
<head>
<title>appendChild Gallery</title>

<style>
body {
font-family: Sans-Serif;
font-size: medium;
padding: 20px;
}
caption: {
	caption-side: bottom;
}
</style>

</head>

<body>

<form action="#">

URL:
<input type="text" name="image" id="idImage" value="" onClick="this.select();"/>
<br><br>
Description:
<input type="text" name="des" id="idDes" value="" onClick="this.select();"/>
<br><br>
<input type="button" id="btn" name="submit" value="Load" />
<br><br>
</form>
	
<script>
	document.getElementById('btn').onclick = function() {
	
		//image
		var val = document.getElementById('idImage').value;
		var img = document.createElement('img');
		src = val,
		img.src = src;
		document.body.appendChild(img);
		img.style.width = "150px";
		img.style.height = "150px";
		img.style.padding = "10px";
		img.style.float = "left";
		img.style.margin = "0px, 50px"; 
      
		//description: this is the part that needs help. 
		var valDes = document.getElementById('idDes').value;
		var des = document.createElement('P'); //also tried 'CAPTION' per specs
		src = valDes,
		des.src = src;
		//valDes.body.appendChild(des); //error "is not a function" ??
		//des.setAttribute('label', valDes);
		//des.setAttribute('style', 'width: 150px;');
		//document.getElementById("description").appendChild(des);
		var tex = document.createTextNode(valDes);
		description.appendChild(tex);
		description.style.display = 'inline-block';
		//description.style.position = 'absolute'; 
		des.style.float = 'left';		
		des.style.width = "150px";
		des.style.height = "160px";
		des.style.padding = "10px";  
		
/* integrate localStorage 
		localStorage.setImage('idDes', valDes);
		localStorage.setImage("idImage", val);
		
		imgStore = localStorage.getImage("idImage");
		desStore = localStorage.getImage("idDes");
		img1 = document.createElement('img1');
		img1.src = imgStore; //stored image
		//desStore.src = desStore;
		document.body.appendChild(img1);
		document.getElementById('caption2').innerHTML = document.getElementById('desStore');
*/
        }
</script>

<p id="description"></p>

</body>

</html>

 

Share this post


Link to post
Share on other sites
Ingolme    794

I don't see an issue on the example page you showed, what is it doing now and what did you expect it to do?

Share this post


Link to post
Share on other sites
paulmo    3
15 minutes ago, Ingolme said:

I don't see an issue on the example page you showed, what is it doing now and what did you expect it to do?

Hi, the captions need to be under (below) the images, as mentioned in first post. Thanks. 

Share this post


Link to post
Share on other sites
Ingolme    794

The image is floated to the left, so all the rest of the content will be on the right of it. You should remove the float property from the image and wrap the image and paragraph into another element.

Share this post


Link to post
Share on other sites
paulmo    3

Hi, so I've removed float property and wrapped the image and paragraph into an element together, but I wouldn't know how to wrap them in another element since they're both already rendered on the page, and document.createElement only accepts 1 parameter.

Anyhow, my attempt is below. Thanks for any help.  

        document.getElementById('btn').onclick = function() {
	
		//image
		var val = document.getElementById('idImage').value;
		var img = document.createElement('img');
		src = val, //must redefine
		img.src = src;
		var imgOut = document.body.appendChild(img);

		img.style.width = "150px";
		img.style.height = "150px";
		img.style.padding = "10px";
		//img.style.float = "left";
		img.style.margin = "0px, 50px"; 

		//description
		var valDes = document.getElementById('idDes').value;
		var des = document.createElement('P'); //also tried 'CAPTION' per specs
		src = valDes,
		des.src = src;
		//valDes.body.appendChild(des); //error "is not a function" ??
		//des.setAttribute('label', valDes);
		//des.setAttribute('style', 'width: 150px;');
		//document.getElementById("description").appendChild(des);
		var tex = document.createTextNode(valDes);
		
		var texOut = description.appendChild(tex);

		var combine = document.createElement('imgOut'.'texOut'); //trying this idea...doesn't work. 
		document.body.appendChild(combine);

		description.style.display = 'inline-block';
		//description.style.position = 'absolute'; 
		//des.style.float = 'left';		
		des.style.width = "150px";
		des.style.height = "160px";
		des.style.padding = "10px";  

 

Edited by paulmo
clarification

Share this post


Link to post
Share on other sites
Ingolme    794

Here's how you created nested elements:

/* Create a container */
var container = document.createElement("div");

/* Create children */
var image = document.createElement("img");
//... Set attributes
var paragraph = document.createElement("p");
// ... Set attributes and content


/* Add children to the container */
container.appendChild(image);
container.appendChild(paragraph);

/* Add the container to the body */
document.body.appendChild(container);

 

  • Like 1

Share this post


Link to post
Share on other sites
paulmo    3

Updated code, same behavior. Thanks for guidance.

document.getElementById('btn').onclick = function() {
    
        //image
        
        var container = document.createElement("div");

        var val = document.getElementById('idImage').value;
        var img = document.createElement('img');
        src = val, 
        img.src = src;

        img.style.width = "150px";
        img.style.height = "150px";
        img.style.padding = "10px";
        //img.style.float = "left";
        img.style.margin = "0px, 50px"; 

        //description
        var valDes = document.getElementById('idDes').value;
        var des = document.createElement('p');
        src = valDes,
        des.src = src;
        
		var tex = document.createTextNode(valDes);

        des.style.display = 'inline-block';
        des.style.position = 'absolute'; 
        des.style.top = '0px';        
        des.style.left = '0px';
        //des.style.height = "150px";
        //des.style.padding = "10px";  
        
		container.appendChild(img);
        container.appendChild(des); //also tried tex
        document.body.appendChild(container);

 

Share this post


Link to post
Share on other sites
dsonesuk    701

des is a paragraph, but a paragraph does not have or use a src attribute?

des.src = src;

It is bad idea to use position: absolute; because it taken out of the flow from the other elements and it occupies no space, unless you have a parent element with position: relative thr property top will take it to the closest position relative parent element, and if none found the top edge of browser window.

If I was you I would do the html first to get it how you want, apply classnames and style it to those classnames.

Then create the same layout using JavaScript adding same classname, you avoid adding all individual style properties that way, as the classnames will take care of that.

Edited by dsonesuk

Share this post


Link to post
Share on other sites
dsonesuk    701

You have to append text node(tex) to des the created paragraph, then append des( which now has textnode within it) to div container

Edited by dsonesuk

Share this post


Link to post
Share on other sites
paulmo    3

Thanks, same behavior with these edits:

	document.getElementById('btn').onclick = function() {
	
		//image
		
		var container = document.createElement("div");

		var val = document.getElementById('idImage').value;
		var img = document.createElement('img');
		src = val, 
		img.src = src;
		//document.body.appendChild(img);

		img.style.width = "150px";
		img.style.height = "150px";
		img.style.padding = "10px";
		//img.style.float = "left";
		img.style.margin = "0px, 50px"; 

		//description
		var valDes = document.getElementById('idDes').value;
		var des = document.createElement('p'); 
		var tex = document.createTextNode(valDes);

		des.style.display = 'inline-block';
		des.style.top = '0px';		
		des.style.left = '0px';
		des.style.height = "160px";
		des.style.padding = "10px";  
		
		des.appendChild(tex); //also tried tex.appendChild(des);
		container.appendChild(img);
		container.appendChild(des);
		//container.appendChild(tex); also tried this
		document.body.appendChild(container);

 

Share this post


Link to post
Share on other sites
dsonesuk    701

Misplaced ',' instead of ' ' in margin styling, and ending semi-colon ';' for 'src = val'


            document.getElementById('btn').onclick = function() {
                var container = document.createElement("div");
                container.style.maxWidth = "270px"; /* just for testing purposes  dsonesuk*/
                container.style.backgroundColor = "red"; /* just for testing purposes  dsonesuk*/
				container.style.display = "inline-block"; /* just for testing purposes  dsonesuk*/
				container.style.verticalAlign = "top"; /* just for testing purposes  dsonesuk*/
                //image
                var val = document.getElementById('idImage').value;
                var img = document.createElement('img');
                src = val; /* replaced ',' with ';' dsonesuk */
                img.src = src;
                //document.body.appendChild(img);

                img.style.width = "150px";
                img.style.height = "150px";
                img.style.padding = "10px";
                //img.style.float = "left";
                img.style.margin = "0px 50px"; /* removed ',' dsonesuk*/

                //description
                var valDes = document.getElementById('idDes').value;
                var des = document.createElement('p');
                var tex = document.createTextNode(valDes);

                des.style.display = 'block';
                des.style.backgroundColor = "green"; /* just for testing purposes dsonesuk*/
                /* des.style.top = '0px';
                 des.style.left = '0px'; removed because obsolete without position used  dsonesuk*/
                des.style.height = "160px";
                des.style.padding = "10px";

                des.appendChild(tex); //also tried tex.appendChild(des);
                container.appendChild(img);
                container.appendChild(des);
                //container.appendChild(tex); also tried this
                document.body.appendChild(container);
            };

With css and class Name

<style type="text/css">
            .img_descrip_wrap {
                display: inline-block;
                max-width: 270px;
                vertical-align: top;
            }
            .img_descrip_wrap img {width:150px; height: 150px; padding:10px; margin: 0 50px; display: block;}
        </style>
  document.getElementById('btn').onclick = function() {

                //image

                var container = document.createElement("div");
                container.className = "img_descrip_wrap";
                var val = document.getElementById('idImage').value;
                var img = document.createElement('img');
                src = val;
                img.src = src;


                //description
                var valDes = document.getElementById('idDes').value;
                var des = document.createElement('p');


                var tex = document.createTextNode(valDes);
                des.appendChild(tex);

                container.appendChild(img);
                container.appendChild(des);
                document.body.appendChild(container);
            };

 

Edited by dsonesuk
  • Like 1

Share this post


Link to post
Share on other sites
paulmo    3

Thanks so much!  It's working great with your revision, and I'll play with the background color section.

Question please: can commented sections /* */ and // interfere with code executing? Because code wasn't executing until I deleted those sections. 

Share this post


Link to post
Share on other sites
dsonesuk    701

It shouldn't if done correctly, you may have an instance where part of code got caught by the commenting, but a good editor would show that, and should have highlighted your previous errors.

Share this post


Link to post
Share on other sites
paulmo    3

gedit doesn't show much in the way of errors, I'm using Chrome(ium) developer console which shows everything. What's the preferred editor here? Thanks

Share this post


Link to post
Share on other sites
dsonesuk    701

I use Netbeans, because its not limited to just 1 or two server languages, but several, including frameworks Zend, smarty, Laravel, Java etc, It gives you traffic light indication to highlight errors in red and warnings in amber, it gives hints, in indicates what closing braces elements belong to. There is also Sublime, but I found Netbeans gives me what I want, and the ability to add more if I want.

  • Like 1

Share this post


Link to post
Share on other sites
paulmo    3

Thanks, researching options, I just installed Visual Studio Code and already enjoying auto-completion.

Edited by paulmo
clarity

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×