Jump to content
Sign in to follow this  
cerstrand_mace

Multiple actions in single if statement (how to separate?)

Recommended Posts

I have this issue with a function where I want to execute 4 different actions inside an if statement. My code looks like this:

function deltagare(){var myTextField1 = document.getElementById('namn');var myTextField2 = document.getElementById('epost'); if(myTextField1.value != "" && myTextField2.value != "")document.getElementById("deltagare-list").value+=myTextField1.value+" "+"("+myTextField2.value+")"+"\n",document.getElementById("deltagare-emaillist").value+=myTextField2.value+",",document.getElementById('namn').value="",document.getElementById('epost').value=""; elsealert("Skriv in både namn och epost!"); }

In this way only the first action gets executed. How can I add the other ones properly?

Share this post


Link to post
Share on other sites

You're supposed to use curly braces to make a code block. W3Schools even teaches it that way. I'm not sure where you learnt Javascript.

if(condition) {    Multiple    lines    of    code} else {    More    code}

Share this post


Link to post
Share on other sites
You're supposed to use curly braces to make a code block. W3Schools even teaches it that way. I'm not sure where you learnt Javascript.
if(condition) {	Multiple	lines	of	code} else {	More	code}

OK, so I added the braces but it did't help. I still only get the first action. And YES I'm new to the whole Javascript shebang, hope that won't get my aid refused :)

Share this post


Link to post
Share on other sites

I have no problem with your inexperience, everybody goes through a phase of learning and I'm glad to help. I just would like to talk with the people who taught you or provided you with the information you learnt from, teaching badly is a disservice to people. The problem you're experiencing after fixing the first mistake probably has to do with ending your lines with a comma (,) instead of a semi-colon( ; )

Share this post


Link to post
Share on other sites
I have no problem with your inexperience, everybody goes through a phase of learning and I'm glad to help. I just would like to talk with the people who taught you or provided you with the information you learnt from, teaching badly is a disservice to people. The problem you're experiencing after fixing the first mistake probably has to do with ending your lines with a comma (,) instead of a semi-colon( ;)
I learn mostly by doing and googling by myself. I'm well aware of my need to get through the basics, but I just haven't had the time yet, sry for that :)Anyway, I'm VERY grateful for your help. I suspected that the semi-colons could be the problem but I've tried that too with no result. Any other ideas?Thank you!

Share this post


Link to post
Share on other sites

If you can show the HTML you're using I can test it on my computer and see what's not working. I'll take a closer look at your current code until then. Update:Here's my test code and it's working:

<!DOCTYPE html><html>    <head>	    <script>	    function deltagare(){		    var myTextField1 = document.getElementById('namn');		    var myTextField2 = document.getElementById('epost');		    if(myTextField1.value != "" && myTextField2.value != "") {			    document.getElementById("deltagare-list").value+=myTextField1.value+" "+"("+myTextField2.value+")"+"\n";			    document.getElementById("deltagare-emaillist").value+=myTextField2.value+",";			    document.getElementById('namn').value="";			    document.getElementById('epost').value="";		    } else {			    alert("Skriv in både namn och epost!");		    }	    }	    </script>	    <title>Test</title>    </head>    <body>	    <div>		    <input type="text" id="namn">		    <input type="text" id="epost">		    <textarea id="deltagare-list"></textarea>		    <textarea id="deltagare-emaillist"></textarea>		    <input type="button" value="Test" onclick="deltagare();">	    </div>    </body></html>

Edited by Ingolme

Share this post


Link to post
Share on other sites
If you can show the HTML you're using I can test it on my computer and see what's not working. I'll take a closer look at your current code until then. Update:Here's my test code and it's working:
<!DOCTYPE html><html>	<head>		<script>		function deltagare(){			var myTextField1 = document.getElementById('namn');			var myTextField2 = document.getElementById('epost'); 			if(myTextField1.value != "" && myTextField2.value != "") {				document.getElementById("deltagare-list").value+=myTextField1.value+" "+"("+myTextField2.value+")"+"\n";				document.getElementById("deltagare-emaillist").value+=myTextField2.value+",";				document.getElementById('namn').value="";				document.getElementById('epost').value="";			} else {				alert("Skriv in både namn och epost!");			}		}		</script>		<title>Test</title>	</head>	<body>		<div>			<input type="text" id="namn">			<input type="text" id="epost">			<textarea id="deltagare-list"></textarea>			<textarea id="deltagare-emaillist"></textarea>			<input type="button" value="Test" onclick="deltagare();">		</div>	</body></html>

http://scooper.se/novo/ama/ama-af-12/ See if you can make something out of this. The script is used to add names and email-adresses to a textarea (along with some other functions connected to a mail system). The textarea can be found in the form displayed by the button that says "Klicka här för anmälan".

Share this post


Link to post
Share on other sites

You seem to be missing a textarea with the ID "deltagare-emaillist". I can't see it on the page, and the error console says it can't find the element either.Javascript normally stops executing when it encounters an error.

Share this post


Link to post
Share on other sites
http://scooper.se/novo/ama/ama-af-12/ See if you can make something out of this. The script is used to add names and email-adresses to a textarea (along with some other functions connected to a mail system). The textarea can be found in the form displayed by the button that says "Klicka här för anmälan".
Your test-code works like a charm btw! So the problem must be somewhere else in my form-system.

Share this post


Link to post
Share on other sites
You seem to be missing a textarea with the ID "deltagare-emaillist". I can't see it on the page, and the error console says it can't find the element either.Javascript normally stops executing when it encounters an error.
Yes! I must have made a very clumsy mistake somewhere on the way. Now it works fine! Thanks alot!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...