2

I'm having some trouble with some javascript. I'm using jQuery to dynamiclly add rows to a table. Additionally the javascript is being generated from some PHP and Kohana code.

Here is the script.

<script type="text/javascript">   
    function delExtensionNumber(element){
        $(element).prev().attr('value', 'X'); 
        $(element).parent().parent().hide();
    }

    function addExtensionNumber(){
        lines = <?php echo form::dropdown($ext_id."[did]", $lines, ''); ?>;
        extensions = <?php echo form::dropdown($ext_id."[extension]", $phones, ''); ?>;

        $('#line-extensions > tbody:last').append('<tr><td>'+lines+'</td><td>'+extensions+'</td><td><input type="hidden" name="'+<?php echo $ext_id;?>+'[state]" value="0" /><a class="hide" onclick="delExtensionNumber(this)"></a></td></tr>');

        <?php $ext_id++; ?>
    }
</script>

This works fine in Firefox. But in IE and Chrome it doesn't work. Nothing happens in google Chrome. In IE I get an error on the page. The error is a syntax error. It doesn't like the < character after the word line.

Ok, so I thought I better put some quotes around this string. But... when I do that nothing works.

1
  • Could you show what is outputted in the firefox version? Commented Jul 5, 2010 at 1:00

3 Answers 3

5

I'm just going to assume that form::dropdown is spitting out plain HTML. In Firefox, this is being interpreted with E4X, and then turned back into a string when concatenated. IE and Chrome do not support E4X. To make it work, you could do something like this:

lines = <?php echo json_encode(form::dropdown(...)); ?>;

Edit: The reason it's not working when you're putting quotes around it is because the HTML contains quotes. The code above will escape the quotes correctly.

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

3 Comments

You are correct, form::dropdown() does return a string of HTML.
Frantastic. Not what I would have thought of but that worked great! thanks alot.
Actually I was pretty careful with the quotes but i still couldn't make it work. So the json encode version did the job.
2

Chrome does not support XML literal syntax, you can't write e.g. line = <div>foo</div>, you have to enclose the data into a string.

lines = '<?php echo form::dropdown($ext_id."[did]", $lines, ""); ?>;'

could work (and you should possibly also escape ')

1 Comment

That's what I thought might work, but actually it doesn't work at all.
-2

I strongly recommend you to use Jquery DOM Creation Plugin

it would be a lot, easier.

2 Comments

I don't see why. The HTML is generated by Kohana, and so I don't see why it would be necessary to imitate that function client-side. For all I know, it might rely on something from the database, and would be impossible to implement client-side.
i misunderstood your question.

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.