I'm using a select menu with numbered options from 0-6. When someone selects option 3, 3 instances of the same paragraph should be appended to the selection. If 5 is selected, 5 paragraphs would be appended. What I'd also like is for the name attribute to be incremented with a number.

The code works as expected but after playing around with it I realize that my choice of append was definitely not the best option. If option 3 is selected 3 paragraphs are created. If option 5 is then selected there should be a total of 5 paragraphs and not 8 paragraphs. Thus option 1 only displays 1 paragraph, option 2 = 2 paragraphs, option 3 = 3 paragraphs etc. The name attribute would also be incremented from the beginning again. The name attribute for option 1's paragraph would be: name="computer[0]"; for option 3 the first paragraph would have name="computer[0]", while the third paragraph would be name="computer[2]" etc...

Do you think it would be a big mission to change the previous code?

Thank you and I hope you're well.

Pullo
—
2014-02-03T20:29:16Z —
#4

Hi RedBishop,

RedBishop said:

Do you think it would be a big mission to change the previous code?.

Nah, not really.

Just build up a string, then replace the HTML of the content div with the newly created string, like so:

I've added the jQuery to my actual page and a number of "issues" have surfaced. The names of my select menus are of the format: name="somename[somename]". Do you know of a technique whereby name = $(this).attr("name"); only retrieves the content between the brackets?

Another problem is that I have multiple select menus on my page each with the same class="someclass". If I select an option from a random select menu, all of the other occurrences of class="content" get the same paragraphs. I'm sure you know what I mean. This is probably solved by sticking next() somewhere?

I appreciate all of your help - thank you.

Pullo
—
2014-02-04T10:40:21Z —
#11

Hi there,

RedBishop said:

I've added the jQuery to my actual page and a number of "issues" have surfaced. The names of my select menus are of the format: name="somename[somename]". Do you know of a technique whereby name = $(this).attr("name"); only retrieves the content between the brackets?

Another problem is that I have multiple select menus on my page each with the same class="someclass". If I select an option from a random select menu, all of the other occurrences of class="content" get the same paragraphs. I'm sure you know what I mean. This is probably solved by sticking next() somewhere?

In your previous example you were appending to a div with the id content.If you want to use a class, we'll need to see the HTML so that the correct selector can be found.

RedBishop
—
2014-02-04T11:02:03Z —
#12

Thank you for getting back to me. I've changed the IDs to classes. The generated HTML would look like this:

If the p element you want to append the content to is directly after the select element, then you were right on the money:

$(this).next().html(newContent);

RedBishop
—
2014-02-04T13:40:50Z —
#14

$(this).next().html(newContent);

Great, it is working well now, thank you very much. One thing that I still have a problem with is the regular expression. Must I substitute 'name' with 'matches' in the 'Created by menu' paragraph? And must I leave console.log in the script?

I promise I won't take up a lot of your time today, but I have a quick query if you don't mind too much.

As you know I have multiple select menus on the page, which I have “looped out” with a while loop. For each select menu I'm also returning some other info:$num_computers = $row['num_computers'];

So depending on how many select menus there are, I'll have an equal number of $num_computers, each with a different value (1, 5, 6) etc.

I know I can usevar count = $(this).val(), to get the specific value of the specific select menu option, but how to get the associated $num_computers value?

Usingvar computer = '<?php echo $num_computers;?>'; only retrieves the last $num_computers on the page.

I hope all of this makes somewhat sense!

Thank you in advance.

Pullo
—
2014-02-05T20:34:26Z —
#18

Hi Red Bishop,

Sorry I didn't respond sooner, the email notifications have slowed to a crawl for some reason and I missed this.

$num_computers looks like a PHP variable.Where are you storing it?If you need to associate this variable with each select element, you might consider storing it as a data attribute on each element.You would do this in your while loop e.g.

In the while loop I'm assigning each store's $row['num_computers']; to $num_computers, which I then echo out. But instead of echoing it out I'm now trying to use it to create another select menu with numbered options. If a specific $num_computers has a value of 5, it would create a select menu with numbered options from 0-5.

If you need to associate this variable with each select element, you might consider storing it as a data attribute on each element.

I'm using HTML4, so this probably won't work. I got the second select menu working but it's not retrieving the specific $num_computers value.

var computer = '<?php echo $num_computers;?>';

only returns the last $num_computers value. Don't know what to do.

Thank you.

Pullo
—
2014-02-06T12:30:33Z —
#20

RedBishop said:

In the while loop I'm assigning each store's $row['num_computers']; to $num_computers, which I then echo out. But instead of echoing it out I'm now trying to use it to create another select menu with numbered options. If a specific $num_computers has a value of 5, it would create a select menu with numbered options from 0-5.

So that's PHP, right?

Once you have a reference to $num_computers for the individual stores, couldn't you just use it to create a for loop?