Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

document.getElementById not working

wakespirit
Kilo Guru

Dear all,

I have a record producer that I am displaying in a GlideModal using below script :

var gform=new GlideModal('test_producer',false,'modal-lg');

Then I have a UI page named "test producer" define as below :

HTML script :

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
		<button id="close" onclick="closeAck()" type="button">$[gs.getMessage("Close")]</button>
	<div>
	
	<iframe id="rp_frame" src="/com.glideapp.servicecatalog_cat_item_view.do?v=1${AMP}sysparm_id=a57bed012f8ba30061999bacf699b64e" width="800" height="600">
		</iframe>
		
	</div>
<script>

addLoadEvent( function() {
onLoadFunction();

});


</script>	
</j:jelly>

Client Script :

function closeAck() {
	alert("CLOSE ACK REACH");
	GlideDialogWindow.get().destroy();
	//saveProducer();
}

function onLoadFunction()
{
   alert("FRAME LOADED");
	var iframe = document.getElementById('rp_frame');
	 if(iframe){
       
	 var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
	  var button = innerDoc.getElementById("submit_button");
		 if(button){
		   var mybuttonValue= button.value;
			 	alert("SUBMIT BUtton : " + mybuttonValue)  ;
		 }
		 else{
			 alert("SUBMIT BUTTON NOT FOUND")  ;
		}
    }
	else{
			 alert("IFRAME NOT FOUND")  ;
	}
	
}

What I am trying to do is to get the Submit_button of the record procuder which is embeded in the Iframe.

For some reason I cannot reach the buttton id using getElement, the alert message "SUBMIT BUTTON" is displayed

What i am doing wrong ? does it means the button is not yet ready when onload method is called ? how should I change my script to be able to reach the submit button 
?

Below is the debug view element of my button I need to catch :

<button type="button" id="submit_button" name="submit_button" class="request_catalog_button header_button btn cat-btn btn-primary" href="#" onclick="saveProducer('');" title="" aria-label="Submit" data-html="true" role="button" data-original-title="Submit">
			Submit
			<span class="sr-only">Submit</span></button>

 

once I am able to access this button, I need to register to the onclick event in order to run a script, but as you can see from below the click event of the button is already calling saveProcuder(''), can I register to this event and run an other script in parallel ?

Thnaks to help me to know why i cannot fetch my button and register to its click event

regarsd

13 REPLIES 13

could you clarify '

you mean I need to create a method in a script include like :

ClosePopup()

{

GlideWindow.get().destroy()

}

and then on my Record producer onSubmit client script, I call ClosePopup ?

Is this what you mean ?

 

If this is the case, then it does not work, it hangs right away when i click on submit button of my record producer

Dear omar,

could you please clarify how should I write this Ajax script in my Record Producer onSubmit client script ?

regards

Shobhit Sharma
Giga Contributor

You can try something like this . Like you have submit button on incident .

Then the id will be   incident.submit_button .

Try this ......

that does not work because the button is part of the producer record , different context. As I said in debug html button id is written submit_button