Mr Brimm
Mr Brimm

Reputation: 133

Use jquery to add params to video code

I am trying to create a page where users paste their video code and hit the go button. When they do this I want my params to be appended to the params they paste. I cannot seem to figure out how to grab the existing value and add the params. This is inside an object tag. Any help is appreciated. Here's the code:

Enter code for video:<br />
    <textarea rows="15" cols="50" name="code" id="code"></textarea><br />
    <button id="go">Go</button><br /><br />
    <script>

    jQuery('#go').click(function() {
    $("#code").val(function( i, vals ) {
        var myParams = "<param name=\"includeAPI\" value=\"true\" />";
        var $codes = $(vals).find('param').appendTo(myParams);

        vals += $codes;
          return vals;
        });
      });

    </script>

This script just gives me the original paste plus [object Object] at the end.

Snip of video code

<object id="video" class="video_page">
<param name="bgcolor" value="#FFFFFF" />
<param name="width" value="480" />
<param name="height" value="270" />
<param name="playerID" value="1412222" />
<param name="playerKey" value="556555dw4ff4r" />
</object>

Upvotes: 0

Views: 317

Answers (4)

HMartch
HMartch

Reputation: 728

I believe the following script will give you the desired result:

<script>

    jQuery('#go').click(function() {
    $("#code").val(function( i, vals ) { 
       var myParams = '<param name=\"includeAPI\" value=\"true\" />';
       vals = vals.substring(0,vals.indexOf('</object>'));
        vals += myParams + '\n</object>';
        return vals;
        });
      });

    </script>

Edit: variation to handle additional input:

<script>

    jQuery('#go').click(function() {
    $("#code").val(function( i, vals ) { 
       var myParams = '<param name=\"includeAPI\" value=\"true\" />';
       valend = vals.substring(vals.indexOf('</object>'));
       vals = vals.substring(0,vals.indexOf('</object>'));
        vals += myParams + valend;
        return vals;
        });
      });

    </script>

Upvotes: 1

charlietfl
charlietfl

Reputation: 171689

You are trying to concatenate an object with a string in this line:

 vals += $codes;

All you need to get the value appended in the textarea is:

    jQuery('#go').click(function() {
        $("#code").val(function(i, vals) {
            var myParams = "\n<param name=\"includeAPI\" value=\"true\" />";
            vals += myParams;
            return vals;
        });
    });

DEMO: http://jsfiddle.net/nxQ7g/

EDIT: For browser to recognize new param in object you will likely have to replace the existing object with a new and revised one. Add to above code:

var object=$('<object>').attr({id: 'video', class:'video_page'}).html($("#code").val());

$('#video').replaceWith( object)

Upvotes: 0

Frenchi In LA
Frenchi In LA

Reputation: 3169

$('#video').append($('<param />').attr({'name':yourName, 'value':yourVal}));

will add a parameter to your video

Upvotes: 0

zod
zod

Reputation: 12437

Use .add()

http://api.jquery.com/add/

Upvotes: 0

Related Questions