3

I have an odd requirement where we have static html/css/images. I then need to convert these static pages into pages that are dynamically generated with javascript/jquery. We are dealing with 100+ instances of this.

So for example:

<div id='foo' title='my title'>Hi there!</div>

would be converted to:

$('<div/>', {
    id: 'foo',
    title: 'my title',
    text: 'Hi there!'
}).appendTo('#mySelector');

Is there a way to do this programmatically? I googled around but was not able to find anything.

6
  • 1
    This seems like a very strange thing to do. You should generally try to do as much as possible with static HTML, and only use Javascript for the parts that need to be generated dynamically, like tables based on runtime data. Commented Feb 14, 2015 at 2:00
  • 2
    You don't need to use objects like that, you can simply do $("#mySelector").append("<div id='foo' title='my title'>Hi there!</div>");. This keeps most of the structure of the original HTML. You may be able to do this with global replace in the editor. Commented Feb 14, 2015 at 2:02
  • i know it is odd. We have an odd requirement. That is why I'm asking the question. Commented Feb 14, 2015 at 2:09
  • Unfortunately, you're asking in the wrong place. Third-party software recommendations are explicitly off-topic. Commented Feb 14, 2015 at 2:10
  • You could load a script that parses the DOM into a json string, and then manually copy and paste that to save your own .json file. From there, you could use JavaScript to load the pieces from the .json file you want into the DOM. Commented Feb 14, 2015 at 2:52

1 Answer 1

1

Something like this?

<div></div>

$('div').append('<p></p>');
$('div p').attr({
    'id':'foo',
    'title':'my title'
}).text('Hi there!');

OUTPUT :

<div><p id="foo" title="my title">Hi there!</p></div>
Sign up to request clarification or add additional context in comments.

2 Comments

I want to go the other way. HTML -> JS
yeah. And I'm looking for a way to automate it

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.