Skip to content Skip to sidebar Skip to footer

Add Html Blocks

I have an asp.net web form that gathers details about clinics and patients. On the bottom part of the form i have a block that basically asks for patient information (firstname,las

Solution 1:

use the $.clone method

here is an example

http://jsfiddle.net/eTh2f/1/

make sure you don't use ID though , cuz you will end up with duplicate ids


Update :

  • i see that you dont really understand the concept in here . ID's are ment to be unique (think of it as a government ID number ) not two can have the same ID.

  • while classes are not unique (just like names , many people share the same name ) , they can be assigned to more than one element .

when to use which

use id's for elements that should never be repeated on the same page . for example the main header can have an id of id="header_container" . While the items of a list can never have the same id because they are not unique (notice that we are talking about the tag not it's content).

examples

for id's the header should be ok . so :

<div id="header">... header contents ...</div>

and for classes a list of items should be ok too

<ul><liclass="someone" > me <li><liclass="someone" > you <li><liclass="someone" > he <li></ul>

you CAN NOT do :

<ul><liid="someone" > me <li><liid="someone" > you <li><liid="someone" > he <li></ul>

back to jquery

you can use any type of valid css selector in jquery

so if i were to get the header i will use

$('#header')

where # is the sign for id

and if i were to select ALL the list items

$('.someone')

where . is the sign for id

what will happen if you use multiple ID's

first of all you should know that the use of id's is not a really smart idea . i rarely use them (if you can avoid the trouble then do ) .

so use classes . because they can be still only assigned to one element if you do so . or use the new data attribute .

but if you are 100% sure that they will never repeat then use them . example of that is a set of records in the database . sure thing they dont have the same id then just use the attribute id for the elements.

how can i select a text box in jquery

well simply assign a class . if you dont want to then simply

$('input')

which will select all the input tags , still a bit inconvenient though , but no worries. you can select an input by it's name via

$('input[name="the_name_of_the_input"]')

Finally

please read more about the topic by googling it . i just append to have a free time . so i invested it . but work hard to get the answer thats how it sticks

Solution 2:

I advise you to use multiple HTML inputs, like:

<inputtype="text" name="name[]"/>

Then, in your JavaScript you just append, or clone like the other guy said, the fields! Than you can easily validate in your server side, because every field in your form will be an array, like

(name[0] = 'patient1', name[1] = 'patient2', etc).

Count the number of inputs, and then validate they.

Post a Comment for "Add Html Blocks"