Adjust the font size based on the input field's value using jQuery's .val() method

I am attempting to apply different font-size values based on the input received from $("#company").val() and $("#firstname").val() within the container #page2. I have experimented with both the css() method and external stylesheets, but so far I have not been successful in achieving the desired result.

$(document).ready(function() {
  $("#printid").hide();

  $("#print").click(function() {
    $(".form").hide();
    $("#printid").show();
    $("#page2").html($("#company").val() + "<br />" + $("#firstname").val())
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form">
  <form>
    <p>
      <label for="company">Comp: </label>
      <input id="company" type="text">
    </p>
    <p>
      <label for="name">Name: </label>
      <input id="name" type="text">
    </p>
  </form>
  <input type="button" id="print" value="Skriv Ut" />
</div>

<div id="printid">
  <p id="page2"></p>
</div>

Answer №1

An easy way to achieve this is by enclosing each value in a separate span element with distinct classes, and then utilizing CSS to customize their appearance. Here's an example implementation:

$(document).ready(function() {
  $("#printid").hide();

  $("#print").click(function() {
    $(".form").hide();
    $("#printid").show();
    $("#page2").html('<span class="company">' + $("#company").val() + '</span><br /><span class="name">' + $("#name").val() + '</span>');
  });
});
.company { font-size: 1.5em; }
.name { font-size: 0.8em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form">
  <form>
    <p>
      <label for="company">Company: </label>
      <input id="company" type="text">
    </p>
    <p>
      <label for="name">Name: </label>
      <input id="name" type="text">
    </p>
  </form>
  <input type="button" id="print" value="Print" />
</div>

<div id="printid">
  <p id="page2"></p>
</div>

It's important to note that the input field was labeled as name, while you referenced it as firstname - making it likely just an oversight in the query.

Answer №2

Why not simply utilize css by applying a class to them?

$(document).ready(function() {
  $("#printid").hide();

  $("#print").click(function() {
    $(".form").hide();
    $("#printid").show();
    $("#page2").html($("#company").val() + "<br />" + $("#name").val())
  });
});
#page2{
  font-size:22px;
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form">
  <form>
    <p>
      <label for="company">Comp: </label>
      <input id="company" type="text">
    </p>
    <p>
      <label for="name">Name: </label>
      <input id="name" type="text">
    </p>
  </form>
  <input type="button" id="print" value="Skriv Ut" />
</div>

<div id="printid">
  <p id="page2"></p>
</div>

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

Working with JQuery to extract and append data stored in a JSON object

I am working with the following JSON data: [ { "MOD_AXL": 0, "MOD_CDS_ID": 110000168, "MOD_CV": 0, "MOD_CV_CTM": null, "MOD_ID": 168, "MOD_MFA_ID": 514, "MOD_PC": 1, "MOD_PCON_END": 199007, "MOD_PCON_START": 196303, ...

Set boundaries for the width and height of an image in the input field

Looking to restrict the size of an image in an input field using HTML, CSS, and JavaScript/jQuery. Goal is to maintain a perfect square aspect ratio for profile photo uploads (for example, 200x200 or 300x300). ...

Tips on retrieving the API response using AJAX

Currently, I am attempting to retrieve the API response from Flickr using Ajax in my application built with Laravel. To accomplish this, I have established a specific Route and function dedicated to handling API calls. //Route.php Route::post('/getlo ...

Guide to transforming Tailwind CSS into a class and saving it in a CSS document

Recently, I implemented Tailwind CSS to style my application. It has been a helpful tool, but I am looking to consolidate all the CSS into one file for better organization. I want to keep it separate from the HTML code. My attempt to convert the following ...

What is the best way to create a 6-column grid system without using bootstrap?

I am looking to create a row with 2 columns that should turn into 2 separate rows, each containing one column when viewed on smaller devices. I have researched CSS Grid but find it quite complex. Can someone simplify it for me? Thank you! ...

Unable to render the JSON data that was retrieved from a jQuery AJAX request

I am having trouble displaying JSON data that is returned from an AJAX call. Can someone please assist me? I am new to this. $.ajaxSetup({ cache: false, timeout: 5000 }); //String.prototype.toJSON; var the_object = {}; function concatObject(obj) { ...

Challenges when working with AJAX/jQuery in terms of fetching JSON data and setting dataType

I am currently facing a challenge with my practice of AJAX/jQuery coding. Despite my efforts to learn and improve, I find the concepts of jQuery and AJAX quite perplexing. Specifically, I am struggling to understand dataTypes and how to manage different ty ...

Displaying over 20 columns of data in a webpage can be achieved effortlessly by utilizing the power of jQuery DataTables in conjunction

Hey everyone, I'm currently facing a challenge in displaying more than 20 columns using datatables on a single webpage. My tech stack includes MySQL, jQuery datatables, and HTML. This is my MySQL query: select LOT_LOCATION, `Zone Attribute`, a.LOTID, ...

Use jQuery to permit only numeric characters and the symbol "-" to be entered into a textbox

I have been working on a JQuery script that only allows users to input numbers in a text field. However, I am now trying to modify the code to also allow users to enter "-" (hyphen), but so far it's not working as expected. If anyone can provide assis ...

Customize Bootstrap Navbar to Enable Horizontal Scrolling Specifically for Dropdown Buttons

Specific requirements: - Only buttons should be horizontally scrollable - Dropdown of buttons should appear just below the button without shifting the paragraph below Things I have attempted: - Tried using overflow:hidden/auto, overflow-x:auto, and pos ...

CSS selectors duplication can cause confusion and make code harder to maintain. SCSS

If we have style definitions like the following: .a.b { ... } .a.b.c { ... } Is there a method in SASS/SCSS to prevent duplication of the .a.b part? ...

Retrieve the dimensions of a document using only the URL link

Currently, I am working with vb.net and jquery. A specific issue has arisen during my work process. With the help of jQuery, I am loading various URL links to different documents within an iframe using the following code: document.getElementById('iFr ...

What causes jQuery's .width() method to switch from returning the CSS-set percentage to the pixel width after a window resize?

After exhaustively console logging my code, I have finally identified the issue: I am attempting to determine the pixel width of some nested divs, and everywhere I look suggests that jQuery's .width() method should solve the problem. The complication ...

Having trouble with JQuery Draggable in FireFox only working in an iFrame? Learn how to set the ViewPort to fix

Having trouble with jQuery UI Draggable in FireFox 33.0.2: I followed the example code, but it doesn't seem to be working. The scripts are running, CSS classes are added, event bindings are in place, but dragging won't work. I noticed that when ...

AJAX initiates automatically upon page load and also at specified time intervals

Can anyone guide me on how to make my AJAX work when the document loads and run every 20 seconds? I have managed to set it up for running every 20 seconds, but I'm facing issues with getting it to work when the document is loaded. Any assistance on th ...

Blending images together can obscure surrounding elements

Is there a way to make two images crossfade on hover without hiding the text underneath? I need the text to show below the image, not behind it. Any suggestions on how to solve this issue? #center { text-align: center; } #under { text-align: cente ...

Positioning Images in Bootstrap 4

I'm encountering an issue with Bootstrap 4. The left box isn't aligning properly with the right box. This involves both HTML and CSS. How can I go about resolving this issue? <section> <div class="container"> <div class="row"& ...

"After clicking the button for the second time, the jQuery on-click function begins functioning properly

(Snippet: http://jsfiddle.net/qdP3j/) Looking at this HTML structure: <div id="addContactList"></div> There's an AJAX call that updates its content like so: <div id="<%= data[i].id %>"> <img src="<%= picture %&g ...

Could someone please review my CSS code for the dropdown menu? I'm having trouble centering the navigation menu

As a newcomer to coding in CSS, I have tried the suggested solutions for my issue without success. Any help would be greatly appreciated. My goal is to center my menu on the page while keeping the container background covering the full width available... ...

Due to the feature in VISUAL STUDIO CODE that presents folders and subfolders at the same level

While working on my Angular project, I encountered an issue with creating a subfolder within a folder. Despite trying the same process in Windows Explorer, I faced the same problem of them appearing on the same level. What could be causing this discrepan ...