0

I'm trying to allow users to specify the height and width of an iframe. I would like to allow them to enter a height and width which would populate those values in a string wich they could copy and paste. I'm not sure how to pass that value to the string.

<form id="embed-customize" action="/" style="display:block">
    <fieldset class="embed-size">
    <legend>Size:</legend>
    <div>
    <input id="embed-width" type="text" value="300">
    x
    <input id="embed-height" type="text" value="500">
    </div>
    </fieldset>

</form>
<textarea id="embed-url" rows="3" cols="100"></textarea>
<script>
jQuery(document).ready( function() {
var embedWidth = "";
var embedHeight = "";
var iframe = "";

jQuery('#embedWidth').blur(function(){
  embedWidth = jQuery(this).val();
})

var iframe = '<iframe width = ' + embedWidth + 'px height = ' + embedHeight +'px frameborder = "0" scrolling = "no" src = "http://www.example.com">"></iframe>';
jQuery('#embed-url').val(iframe);
})
</script>

This is what I have so far but it doesn't seem to work. I'm not sure what I'm doing wrong.

2
  • The code for an iframe so users can embed it on their site. Commented Jul 11, 2012 at 4:35
  • Secondly. your input id is embed-width but in the javascript your using #embedWidth Commented Jul 11, 2012 at 4:35

2 Answers 2

1

Try this:

jQuery('#embed-width').blur(function(){
  embedWidth = jQuery(this).val();
  var iframe = '<iframe width = ' + embedWidth + 'px height = ' + embedHeight +'px frameborder = "0" scrolling = "no" src = "http://www.example.com">"></iframe>';
  jQuery('#embed-url').val(iframe);
 });
})
Sign up to request clarification or add additional context in comments.

3 Comments

No wonder it wasn't working. You have assigned the id as 'embed-width' but in the jquery selector 'embedWidth' was being used. Updated the answer.
Thanks for the response. This would limit me to only the width and not the height though, right?
Yes. If u need the change in height jus adapt the current version to check for change in height
0

Give this a shot
http://jsfiddle.net/9zsqQ/

Your input id was not consistent. In the HTML it is #embed-width but in the javascript your using #embedWidth.

Secondly your iframe code had spaces between the atribute name and the value, and some were not wrapped in double quotes. It should have been something like this:

var iframe = '<iframe width="' + embedWidth + 'px" frameborder="0" scrolling="no" src="http://www.example.com">"></iframe>';

Updated the jsfiddle so that it works for both inputs:
http://jsfiddle.net/9zsqQ/2/

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.