Developing custom selectable symbols in a form

Would like some assistance on implementing a voting system for users to select their preferred icon from an icon font used on my website. The icons are displayed via HTML and CSS. How can I achieve this functionality using jQuery, and then display the selected icon on the front end while also passing its value through a form?

Considering I cannot nest an input element inside another input, here is the approach I took:

Started off with this jsFiddle example: http://jsfiddle.net/6NVpL/42/

HTML:

 <div class="iconDisplay">Display's selected icon</div>

 <button class="selectIcon">Select Icon</button>

 <div id="iconSelector">
     <span class="icon-icon1"></span>
     <span class="icon-icon2"></span>
     <span class="icon-icon3"></span>
     <span class="icon-icon4"></span>
     <span class="icon-icon5"></span>
     <span class="icon-icon6"></span>
     <span class="icon-icon7"></span>
     <span class="icon-icon8"></span>
 </div>

JS:

 $(".selectIcon").click(function () {
   $("#iconSelector").fadeToggle();
 });

 $("#iconSelector span").click(function () {
    $(this).click(function(){
        $("#iconSelector").hide();
     });
 });

Answer №1

Perhaps you would find this more suitable for your needs?

I have made some improvements to the click handlers:

$("#selectIconButton").click(function () {
    $("#iconSelector").fadeToggle();
});

$("#iconSelector span").click(function () {
    selectIcon($(this));
});

I have implemented a function to handle the icon selection process. Please note: remove the return; statement and adjust the code to fit your application.

function selectIcon(e){
    var selection = e.attr('class');
    $('#selectedIcon')
        .removeClass()
        .addClass(selection)
        .show();
    $("#iconSelector").hide();
    return;
    $.ajax({
        url: 'urltowebsite',
        type: 'POST',
        data: { selectedIcon: selection }
    });
}

A user interface element has been added to display the selected icon and some CSS modifications have been made to accommodate these changes.

Answer №2

To securely save the data, you must implement a server-side script. If you already have a script in place, follow this procedure:

$("#iconSelector span").click(function () {
    var xthis = $(this);
    xthis.click(function(){
        $("#iconSelector").hide();
        $.post('/saveData','icon='+$(xthis).attr('class'),function(){
            $(".iconDisplay").html(xthis.get(0));
        });
    });
});

Try out the complete code on this fiddle: http://jsfiddle.net/6NVpL/45/

Note: Remember to replace /saveData with the name of your own save script.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for retaining filled data after an incomplete form submission

<html> <head><script type="text/javascript"> function pa(){ var fname = document.getElementById('fname').value; var lname = document.getElementById('lname').value; var email = document. ...

Is it logical to steer users to a different page through a redirect?

This page serves as a logout feature that is activated through a jQuery post using a button. The code for the logout process is as follows: <?php session_start(); session_destroy(); header("Location: new location"); ?> When accessing this page dir ...

How I made my WordPress columns automatically resize with the help of Bootstrap

Currently, my Wordpress template utilizes Bootstrap 2.3.1 with two areas set up using SPAN4 and SPAN8. The resolution is fixed at 1170px, so there's no need to configure the lg component of Bootstrap. I'm looking for assistance in properly confi ...

Guide for updating the background color, font color, and border color in Material UI's autocomplete textfield

How can I customize the appearance of this material-ui autocomplete textfield (combobox) by changing the background color, font color, and border color using CSS? Here is what I have attempted so far: <Autocomplete disablePortal id=" ...

What is the best way to import my json information into an HTML table and customize the rows as radio buttons using only Javascript?

I am facing an issue with processing a JSON response: var jsondata = dojo.fromJson(response); There is also a string that I am working with: var jsonString = jsondata.items; The variable jsonString represents the JSON data in the file: jsonString="[ ...

Issue with October CMS: Radio button selection triggers an Ajax call, but clicking twice in quick succession causes the content

I am currently utilizing October CMS and materializecss to develop a form with options on one of my pages. The form functions correctly as it dynamically loads content when different options are clicked. However, I have identified a problem where clicking ...

The property 'apply' is trying to be accessed but is undefined and cannot be read

Not being an expert in frontend development, I've hit a roadblock on what seems like a simple issue. I'm attempting to load one set of nodes from a JSON file successfully, but when trying to add new nodes from another JSON file, I encounter this ...

top margin is functioning properly in Internet Explorer, but not in Google Chrome

margin-top is behaving differently in IE compared to Google Chrome. Two menus are supposed to be displayed one above the other in my design. The issue lies in the line margin-top:30%; within .anothermenu ul. In Chrome, the second menu appears above the f ...

"Enhance Your Design With CSS3 Styling for Labels and

HTML <div class="select"> <div> <label for="kitty" class="kitty-label kitty-label-1 l-center"> </label> <input type="checkbox" name="cats" value="1"> <label>Kitty One</label> ...

Changing the z-index using createjs

Is there a way to ensure that the dragged item always stays on top when moved? How can I prevent it from getting dragged underneath another object? Can I specify which "sequenceNumbers" should be moved to the top of the sorting order? //++++++++ ...

Is the accept attribute of the input not recognizing application/json as a valid input type?

My application allows users to upload files, including JSON files. While most browsers recognize file extensions specified in the accept attribute of the input element, Safari may not always behave as expected. In cases where MIME types need to be used, sp ...

In an AJAX response, the button will be disabled if any checkboxes are left unchecked, regardless of their group

This text has been inspired by a thread on Stack Overflow regarding disabling a button based on checkbox selection In the original post, the button is disabled unless at least one checkbox is checked. In my scenario, I have two sets of checkboxes: <d ...

Can the JavaScript code be altered within the client's browser?

So, I'm using JQuery Validator on my webform to validate form data. However, I haven't added any validation on the server side. I'm curious if it's possible for a user to modify the code and bypass my validations in their browser. If th ...

Implementing link submission with jQuery AJAX that sends empty values

I want to implement a functionality where, upon clicking the like button, data will be submitted using ajax in jquery and instead of displaying "Like", it should show "Dislike". However, I am encountering the following issues: 1) After submission, it red ...

The requested TYPO3 eID for ajax functionality could not be found and returned a

I am using TYPO3 version 7.6.18 and attempting to set up ajax requests on the front end. I need the ajax request to call a specific plugin controller and action. I have tried different AjaxDispatchers, but now I am encountering a 404 error stating "eID not ...

Incorporating hyperlinks within the navigation buttons

I'm working on a design where I have six buttons that need to be displayed on top of a background image. How can I ensure that the text inside the buttons stays contained within them? Here is the current code structure I am using, which involves utili ...

Incorporate Angular exclusively in pages that are dynamically loaded via ajax requests

<html> <head> <title></title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script> </head> <body> <div id="ajax-content-here"> </div> </body> ...

Combining dropdowns, nav-pills, and separate links in Bootstrap 4 to create a seamless horizontal layout

Trying to achieve a layout in Firefox where everything is displayed in one single horizontal row. This includes the label Dropdown, dropdown box itself, bootstrap nav-pills menu, and separate link. Code snippet: (jsfiddle: https://jsfiddle.net/aq9Laaew/16 ...

Adaptable data table featuring varying sizes of columns and rows

I'm attempting to design a grid that resembles the following: https://i.sstatic.net/Sf7M9.png The challenge I'm facing is how to ensure that the column and row names adjust properly with the grid in the center. Currently, this is the code I hav ...

Trigger an event in JQuery when a selector changes

Here is my HTML code with a file input: <input type="file" id="file"> I initially tried to add an onChange event listener like this: $("#file").change(function(){}); Unfortunately, it did not work as expected. I had to modify the code to make it ...