Solved

if stament to run a html / jquery code

Posted on 2013-01-22
9
187 Views
Last Modified: 2013-03-14
hi, i using the following jquery / html code:
$(document).ready( function (){
	jQuery.get('Wtxt', function(data) {
		var theList = data;
		var arr_theList = theList.split("\n");
		//var newTFOptions = "<option value='000'>Please Choose</option>";
		var arrOptions = ["<option value='000'>Please Choose</option>"];
		for (var x=0; x<arr_theList.length; x++) {
			var arrListItems = arr_theList[x].split("\",\"");
			if (typeof arrListItems[1] != 'undefined') {
				var strText = arrListItems[0].substring(1,arrListItems[0].length);
				var strValue = arrListItems[1].substring(0,arrListItems[1].length-2);

				if (strText != '' && strValue != '') {
					//newTFOptions += "<option value='" + strValue + "'>" + strText + "</option>";
					arrOptions.push("<option value='" + strValue + "'>" + strText + "</option>");
				}
			}
		}		
		$('#RDS').html(arrOptions.join()).trigger("liszt:updated");

		//Value database
		var currentValue = '<#ITM_FIELD dsname="RDS" ds="content">';
		if ( currentValue != '' ) {
			document.getElementById('RDS').value = currentValue;
			$('#RDS').trigger('liszt:updated');
		}
	});
});


</script>

<!-- display wire types -->
<!-- input type="hidden" name="RDS" value="<#itm_text name="itmc2_process_2_desc">" -->
<div class="vf__required group">
    <label for="RDS">Payment Type</label>
	<select id="RDS" name="RDS">
	</select>
    <script>
	jQuery(function ($) {
	    $("select[name='RDS']").addClass("vf__one").css({"height":27});
	});
    </script>
</div>

Open in new window

how can i include the above code in an if statement to work conditionally.
example of what i need is:

if chekvalue == 0005 i want it all to work else don't display in order to be used .
0
Comment
Question by:eaweb
  • 3
  • 3
  • 3
9 Comments
 
LVL 9

Expert Comment

by:Sar1973
ID: 38809337
Wht don't you include the whole function inside the statement? This will launch the test at each function trigger/fire.
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38809616
Hmm, your problem is to make up an if clause?

if (  checkvalue == '0005' ) {
   // do something here
} else {
   // do another thing here

}

Open in new window

0
 

Author Comment

by:eaweb
ID: 38809697
no my problem is:

if checkvalue == '0005'  i  don't want the html label and select to be created.
instead of the select and the label i need a hidden input field to be created

if (  checkvalue == '0005' ) {
   don't create display the label and the select
<label for="RDS">Payment Type</label>
      <select id="RDS" name="RDS">
      </select>
} else {
   create the following hidden field to be submitted with the form
<input type="hidden" name="RDS" value="0004" -->

}

this without creating an error for the jquery script used to populate the select etc...
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 38809728
You can then inner the new HTML in the other case.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:eaweb
ID: 38809788
how must i modify the code i am using in order not to cause errors
0
 
LVL 11

Expert Comment

by:mcnute
ID: 38809865
In the html leave your <div class="vf__required group">... </div>

The next step is the checkvalue. It is not clear out of your description where this value comes from. Is this a value in your text file you are looping there?

Show us the whole page. Best online, or attach it here with the text file. Then we can debug your code effectively. Now we can just guess what's happening.

Right now there is no code which is checking for "checkvalue" in your script so how do we know where to help you there.
0
 

Author Comment

by:eaweb
ID: 38809997
Hi, sorry for the missing information.

The checkvalue is a database value. that is why i put a standart number.

i need the if statement for

if (  checkvalue == '0005' ) {
   don't create display the label and the select
<div class="vf__required group">
    <label for="RDS">Payment Type</label>
      <select id="RDS" name="RDS">
      </select>
    <script>
      jQuery(function ($) {
          $("select[name='RDS']").addClass("vf__one").css({"height":27});
      });
    </script>
</div>
} else {
   create the following hidden field to be submitted with the form
<input type="hidden" name="RDS" value="0004" -->

}

this without creating an error for the jquery script used to populate the select etc...
0
 
LVL 9

Accepted Solution

by:
Sar1973 earned 350 total points
ID: 38810691
Try to cut and paste this code in the W3 editor:
<!DOCTYPE html>
<html>
<body>

<p>This is some text.</p>

<div id="myDiv" style="color:#0000FF">
  <h3>This is a heading in a div element</h3>
  <p>This is some text in a div element.</p>
</div>
  <input type="submit" value="Submit" onclick="myFunction()">

<p>This is some text.</p>

<script>
 function myFunction() {
  var myHTML=document.getElementById("myDiv").outerHTML;
  var myHTMLAdd="<input type='text' name='LastName' value='Mouse'>";
document.getElementById("myDiv").innerHTML=myHTML+myHTMLAdd;
  }
</script>

</body>
</html>

Open in new window

0
 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 150 total points
ID: 38810918
If RDS is not created jquery will throw an error because it cannot find the element and say RDS is undefined.

I would put this section here
 <script>
      jQuery(function ($) {
          $("select[name='RDS']").addClass("vf__one").css({"height":27});
      });
    </script>

Open in new window


together with the part of code which creates the select and the label. Since this is only needed when the label and the RDS select is created. I'm not sure if this is what causes the error but this is an issue which will be there when you have created the if statement and the value is not 0005. For in that case jquery will try modify the height for an element which is not there.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now