0

I have an iframe like this:

<iframe name="report-iframe" id="report-iframe" src="https://reporting.com/embed" width="100%" height="300" frameborder="0"></iframe>

I want to replace the src value using values from a form

<form method="post" target="report-iframe">
<input id="form-type" name="form-type" type="text" />
<input id="form-date" name="form-date" type="date" />
<input type="button" value="Update Report" onclick="javascript_function()"/>
</form>

so the resulting source of the iframe is:

src="https://reporting.com/embed?param_type=form-type&param_date=form-date"

Then reload the iframe with the passed parameters.

I want to do this using only javascript/jquery if possible.

1
  • We can't do that for you, but there is already a topic on this question Commented Oct 20, 2016 at 20:03

2 Answers 2

0

Here's a jQuery approach.

function javascript_function() {
  $('#report-iframe').attr('src','https://reporting.com/embed?param_type=' + $('#form-type').val() + '&param_date=' +  $('#form-date').val());    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<iframe name="report-iframe" id="report-iframe" src="https://reporting.com/embed" width="100%" height="300" frameborder="0"></iframe>


<form method="post" target="report-iframe">
<input id="form-type" name="form-type" type="text" />
<input id="form-date" name="form-date" type="date" />
<input type="button" value="Update Report" onclick="javascript_function()"/>
</form>

Sign up to request clarification or add additional context in comments.

Comments

0

This JSFiddle accomplishes what you're asking. You'll want to tweak it for robustness, but basically you can target the form (I used $('form') but for a real-world scenario you might want to be more specific.) and use .serialize() to turn its values into a query string, then append that to the src attribute of your iframe.

Edit: Note that for your specific example to work with .serialize(), you'd want the name attribute of #form-type to be "param_type" and the name attribute of #form-date to be "param_date"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.