Jump to content

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


cerstrand_mace
 Share

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?

Link to comment
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}

Link to comment
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 :)
Link to comment
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( ; )

Link to comment
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!
Link to comment
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
Link to comment
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".
Link to comment
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.

Link to comment
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.
Link to comment
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!
Link to comment
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
 Share

×
×
  • Create New...